muditjain

Range count divisors sum

Jan 23rd, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  6. #define int long long int
  7. #define pb push_back
  8. #define ff first
  9. #define ss second
  10. const int mod = 1e9 + 7;
  11. const int n = 100002;
  12.  
  13. signed main()
  14. {
  15.     ios_base::sync_with_stdio(0);
  16.     cin.tie(0);
  17.     cout.tie(0);
  18.    
  19.    
  20.     int q;
  21.     cin>>q;
  22.     vector<int> sp(n,0), nod(n,1);
  23.     for(int i = 0; i < n; i++){
  24.         sp[i] = i;
  25.     }
  26.     for(int i = 2; i < n; i++){
  27.         if(sp[i] == i){
  28.             for(int j = i*i; j < n; j+=i){
  29.                 if(sp[j] == j){
  30.                     sp[j] = i;
  31.                 }
  32.             }
  33.         }
  34.     }
  35.    
  36.     for(int i = 2; i < n; i++){
  37.         int d = 1;
  38.         int num = i;
  39.         while(num != 1){
  40.             int cnt = 0;
  41.             int x = sp[num];
  42.             while(num%x == 0){
  43.                 cnt++;
  44.                 num/=x;  
  45.             }
  46.             d*=(cnt+1);
  47.         }
  48.         nod[i] = d;
  49.     }
  50.     nod[0] = 0;
  51.     for(int i = 1; i < n; i++){
  52.         nod[i]+=nod[i-1];
  53.     }
  54.    
  55.     while(q--){
  56.        
  57.         int l,r;
  58.         cin>>l,r;
  59.         cout<<1<<endl;
  60.         cout<<nod[r]-nod[l-1]<<endl;
  61.         cout<<2<<endl;
  62.     }
  63.     return 0;
  64. }
  65.  
  66.  
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment