标签:
2 1 10 2 3 15 5
Case #1: 5 Case #2: 10
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 vector<LL>g; 5 LL solve(LL x,LL y) { 6 g.clear(); 7 for(LL i = 2; i*i <= y; ++i) { 8 if(y%i == 0) { 9 while(y%i == 0) y /= i; 10 for(int j = g.size()-1; j >= 0; --j) 11 if(abs(i*g[j] <= x)) g.push_back(-i*g[j]); 12 g.push_back(i); 13 } 14 } 15 if(y > 1) { 16 for(int i = g.size()-1; i >= 0; --i) 17 if(abs(y*g[i]) <= x) g.push_back(-y*g[i]); 18 g.push_back(y); 19 } 20 LL ret = 0; 21 for(int i = g.size()-1; i >= 0; --i) ret += x/g[i]; 22 return x - ret; 23 } 24 int main() { 25 int t,cs = 1; 26 LL a,b,n; 27 scanf("%d",&t); 28 while(t--){ 29 scanf("%I64d%I64d%I64d",&a,&b,&n); 30 printf("Case #%d: %I64d\n",cs++,solve(b,n) - solve(a-1,n)); 31 } 32 return 0; 33 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4802017.html