题目:http://acm.hdu.edu.cn/showproblem.php?pid=1695
2 1 3 1 5 1 1 11014 1 14409 9
Case 1: 9 Case 2: 736427HintFor the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn=1e6+10; typedef long long LL; LL prime[maxn],top; bool notprime[maxn]; void getprime(){ top=0; for(LL i=2;i<maxn;i++){ if(!notprime[i])prime[top++]=i; for(LL j=0;j<top&&prime[j]*i<maxn;j++){ notprime[prime[j]*i]=1; if(i%prime[j]==0) break; } } } LL factor[1005],faccnt; void resolve(LL x){ faccnt=0; for(int i=0;prime[i]*prime[i]<=x;i++){ if(x%prime[i]==0){ factor[faccnt++]=prime[i]; while(x%prime[i]==0){ x/=prime[i]; } } } if(x>1)factor[faccnt++]=x; } int euler[100010]; void getEuler() { euler[1] = 1; for(int i = 2;i <= 100000;i++) if(!euler[i]) for(int j = i; j <= 100000;j += i) { if(!euler[j]) euler[j] = j; euler[j] = euler[j]/i*(i-1); } } int main() { //freopen("cin.txt","r",stdin); getprime(); getEuler(); LL t,ca; LL a,b,c,d,k; LL kb,kd; cin>>t; for(ca=1;ca<=t;ca++){ scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k); if(k==0||k>b||k>d) { //除以0会出现0x0000005c的错误 printf("Case %lld: 0\n",ca); continue; } if(b>d) swap(b,d); b=b/k; d=d/k; LL ans=0; for(int i=1;i<=b;i++){ ans+=euler[i]; } for(int i=b+1;i<=d;i++){ resolve(i); LL q1=0; for(int j=1;j<(1<<faccnt);j++){ LL temp=1,sum=0; for(int k=0;k<faccnt;k++){ if((1<<k)&j){ sum++; temp*=factor[k]; } } if(sum&1) q1=q1+b/temp; else q1=q1-b/temp; } ans=ans+b-q1; } printf("Case %lld: %lld\n",ca,ans); } return 0; }