HDU5514 Frogs
1 /* 2 HDU5514 Frogs 3 http://acm.hdu.edu.cn/showproblem.php?pid=5514 4 容斥原理 5 * 6 * 7 */ 8 #include <cstdio> 9 #include <cmath> 10 #include <algorithm> 11 //#define test 12 using namespace std; 13 const long long Nmax=1e5; 14 long long n,m,a[Nmax]; 15 long long book[Nmax]; 16 long long p[Nmax]; 17 int cnt; 18 long long num[Nmax]; 19 long long gcd(long long a,long long b) 20 { 21 if(b==0LL) 22 return a; 23 return gcd(b,a%b); 24 } 25 26 int main() 27 { 28 long long t; 29 #ifdef test 30 while(1) 31 { 32 long long a,b; 33 scanf("%lld%lld",&a,&b); 34 printf("%lld\n",gcd(a,b)); 35 } 36 #endif 37 scanf("%lld",&t); 38 for(long long ttt=1;ttt<=t;ttt++) 39 { 40 scanf("%lld%lld",&n,&m); 41 int flag=0; 42 for(long long i=1;i<=n;i++) 43 { 44 scanf("%lld",&a[i]); 45 a[i]=gcd(a[i],m); 46 if(a[i]==1) 47 flag=1; 48 } 49 if(flag) 50 { 51 long long ans=(m-1LL)*m/2LL; 52 printf("Case #%lld: ",ttt); 53 printf("%lld\n",ans); 54 continue; 55 } 56 long long ans=0LL; 57 cnt=0; 58 for(int i=2;i*i<=m;i++) 59 { 60 if(m%i) 61 continue; 62 p[++cnt]=i; 63 if(i*i!=m) 64 p[++cnt]=m/i; 65 } 66 sort(p+1,p+1+cnt); 67 for(int i=1;i<=cnt;i++) 68 book[i]=num[i]=0; 69 for(int i=1;i<=n;i++) 70 { 71 for(int j=1;j<=cnt;j++) 72 if(p[j]%a[i]==0) 73 book[j]=1; 74 } 75 for(int i=1;i<=cnt;i++) 76 { 77 if(book[i]!=num[i]) 78 { 79 long long tmp=m/p[i]; 80 ans+=tmp*(tmp-1LL)/2LL*p[i]*(book[i]-num[i]); 81 tmp=book[i]-num[i]; 82 for(int j=i+1;j<=cnt;j++) 83 if(p[j]%p[i]==0) 84 num[j]+=tmp; 85 } 86 } 87 88 printf("Case #%lld: ",ttt); 89 printf("%lld\n",ans); 90 } 91 return 0; 92 }