Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 626 Accepted Submission(s): 234
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<cstdlib> 7 #define ll long long 8 using namespace std; 9 10 ll ans,l,r,k; 11 int num,cnt; 12 ll a[107],prime[100007]; 13 bool boo[100007]; 14 int Case; 15 16 void init_prime() 17 { 18 for (int i=2;i<=100000;i++) 19 { 20 if (!boo[i]) prime[++cnt]=i; 21 for (int j=1;j<=cnt&&prime[j]*i<=100000;j++) 22 { 23 boo[prime[j]*i]=1; 24 if (i%prime[j]==0) break; 25 } 26 } 27 } 28 void devide_prime() 29 { 30 num=0; 31 for (int i=1;i<=cnt;i++) 32 { 33 if (k%prime[i]==0) 34 { 35 a[++num]=prime[i]; 36 while(k%prime[i]==0) k/=prime[i]; 37 } 38 } 39 if (k>1) a[++num]=k; 40 } 41 void query(int deep,ll shu,ll qz,ll zhi) 42 { 43 if (deep>num) 44 { 45 ans+=qz*zhi/shu; 46 return; 47 } 48 query(deep+1,shu*a[deep],-qz,zhi); 49 query(deep+1,shu,qz,zhi); 50 } 51 int main() 52 { 53 init_prime(); 54 int cas; 55 scanf("%d",&cas); 56 while(cas--) 57 { 58 scanf("%lld%lld%lld",&l,&r,&k); 59 devide_prime();ans=0; 60 query(1,1,1,r); 61 query(1,1,-1,l-1); 62 printf("Case #%d: %lld\n",++Case,ans); 63 } 64 }