Advertisement
Eather

divisors

Jul 10th, 2011
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1.  
  2. # include <list>
  3. # include <bitset>
  4. # include <algorithm>
  5. # include <sstream>
  6. # include <iostream>
  7. # include <cstdlib>
  8. # include <ctime>
  9. # include <set>
  10. # include <map>
  11. # include <cmath>
  12. # include <queue>
  13. # include <limits>
  14. # include <stack>
  15. # include <vector>
  16. # include <cstring>
  17. # include <cstdio>
  18. # include <fstream>
  19. using namespace std;
  20.  
  21. int pel(string s){string t;t=s;reverse(t.begin(),t.end());if(s==t)return 1;return 0;}
  22. string toString(int n){ostringstream ost;ost<<n;ost.flush();return ost.str();}
  23. int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
  24. bool isprime(int m){if(m<2) return 0;for( int i=2; i*i<=m ; i++)if(m%i==0)return 0; return 1;return 0;}
  25.  
  26. # define __(array,w)   memset(array,w,sizeof array)
  27. # define FOR(i, a, b) for (int i=a; i<b; i++)
  28. # define REP(i, a) FOR(i,0,a)
  29. # define all(c) (c).begin(), (c).end()
  30. # define sz(x) x.size()
  31. # define MAX INT_MAX
  32. # define pb push_back
  33. # define MP make_pair
  34. # define X first
  35. # define Y second
  36. # define SBS(s,a,b) (s).substr(a,b)
  37. # define UNQ(s) {sort(all(s));(s).erase(unique(all(s)),s.end());}
  38. # define rive(s) reverse(s.begin(),s.end())
  39. # define VI vector<int>
  40. # define VS vector<string>
  41. # define VC vector<char>
  42. # define out(a) cout<<#a<<"= "<<a<<endl;
  43.  
  44. map<int, int>gonona,asolgonona;
  45. map<int, int> precal;
  46.  
  47. long long divisors(long long n)
  48. {
  49.  
  50.     long long divv=1;
  51.     long long  i;
  52.     if(n==1){asolgonona[1]=1;return 1;}
  53.     VI vi,vj;
  54.     long long int saved=n;
  55.     for(i=2;i<=(long long )sqrt(n);i++)
  56.     {
  57.         while(n % i == 0)
  58.         {
  59.             n /= i;
  60.             gonona[i]++;
  61.             vi.pb(i);
  62.         }
  63.  
  64.         if(n!=saved)divv*= ((gonona[i])+1);
  65.     }
  66.     if (n >1) {
  67.         divv*=2;vi.pb(n);}
  68.  
  69.     long long c=1;
  70.     sort(all(vi));
  71.  
  72.     REP(i,sz(vi))
  73.     {
  74.         int p=vi[i];
  75.         vj.pb(p);
  76.  
  77.         FOR(j,i+1,sz(vi))
  78.         {
  79.             p*=vi[j];
  80.             vj.pb(p);
  81.             vj.pb(vi[i]*vi[j]);
  82.             vj.pb(saved/vi[j]);
  83.         }
  84.     }
  85.     UNQ(vi);
  86.  
  87.     REP(i,sz(vi))
  88.     {
  89.         int p=pow(vi[i],gonona[vi[i]]);
  90.  
  91.         FOR(j,i+1,sz(vi))
  92.         {
  93.             vj.pb(p*vi[j]);
  94.         }
  95.     }
  96.     gonona.clear();
  97.     sort(all(vj));
  98.  
  99.     UNQ(vj);
  100.  
  101.     REP(i,sz(vj))
  102.     {
  103.         c+=vj[i];
  104.  
  105.     }
  106.     asolgonona[saved]=c;
  107.  
  108.     return divv;
  109. }
  110.  
  111. void precalcu()
  112. {
  113.  
  114.     REP(i,100000)
  115.     {
  116.  
  117.         precal[i+1]=divisors(i+1);
  118.         //cout<<i+1<<" "<<divisors(i+1)<<" "<<asolgonona[i+1]<<endl;
  119.     }
  120. }
  121. int main()
  122. {
  123.     //freopen("testout.txt","w",stdout);
  124.     precalcu();
  125.  
  126.  
  127.     int test;
  128.     cin>>test;
  129.     while(test--)
  130.     {
  131.         int a,b,k;
  132.         cin>>a>>b>>k;
  133.         long long D=0,H=0;
  134.         for(int i=a;i<=b;++i)
  135.         {
  136.             if(i%k==0)
  137.             {
  138.                 D+=divisors(i);
  139.                 H+=asolgonona[i];
  140.             }
  141.         }
  142.         cout<<D<<" "<<H<<endl;
  143.         asolgonona.clear();
  144.     }
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement