标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2151 Accepted Submission(s): 955
1 //合数分解 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define ll long long 7 const int maxn = 1000005; 8 int prime[maxn]; 9 bool pri[maxn]; 10 int cnt; 11 void init() 12 { 13 cnt = 0; 14 pri[0] = pri[1] = 1; 15 //prime[0] = 2; 16 for(int i = 2; i < maxn; i++){ 17 if(!pri[i]){ 18 19 prime[cnt++] = i; 20 for(int j = i+i; j < maxn; j+=i) 21 { 22 pri[j]=1; 23 } 24 } 25 } 26 return; 27 } 28 ll m1[10005],m2[10005]; 29 ll c1[10005],c2[10005]; 30 int main() 31 { 32 init(); 33 int T; 34 ll G,L; 35 scanf("%d",&T); 36 while(T--) 37 { 38 memset(m1,0,sizeof(m1)); 39 memset(c1,0,sizeof(c1)); 40 memset(m2,0,sizeof(m2)); 41 memset(c2,0,sizeof(c2)); 42 scanf("%lld%lld",&G,&L); 43 int tm = 0; 44 bool in = 0; 45 bool fl = 1; 46 //printf("%d\n",prime[1]); 47 if(L%G) fl = 0; 48 for(int i = 0; i< cnt; i++){ 49 while(prime[i]<=L&&L%prime[i]==0){ 50 L = L/prime[i]; 51 m2[tm] = prime[i]; 52 c2[tm]++; 53 in = 1; 54 } 55 if(in) tm++; 56 in = 0; 57 } 58 if(L!=1){ 59 m2[tm] = L; 60 c2[tm] = 1; 61 tm++; 62 } 63 for(int i = 0; i < tm; i++){ 64 while(m2[i]<=G&&G%m2[i]==0){ 65 G = G/m2[i]; 66 m1[i] = m2[i]; 67 c1[i]++; 68 } 69 in = 0; 70 } 71 if(G!=1){ 72 fl = 0; 73 } 74 /*puts("haha"); 75 for(int i = 0; i < tm; i++){ 76 printf("m1[%d]=%d; m2[%d]=%d;\n",i,m1[i],i,m2[i]); 77 printf("c1[%d]=%d; c2[%d]=%d;\n",i,c1[i],i,c2[i]); 78 } 79 */ 80 if(fl==0) {puts("0"); continue;} 81 ll ans = 1; 82 for(int i = 0; i < tm; i++){ 83 //需要特判当c1[i] =c2[i]的情况 84 ll E; 85 if(c1[i]==c2[i]) E = 0; 86 else E = c2[i]-c1[i]-1; 87 /* 88 也可以用排列组合的想法,每次找到两个数包含首尾的两个值,然后中间的一个在从所有可能中选取一个。 89 这样最后的公式就是A(3,2)*(c2[i]-c1[i]-1); 90 所以 91 if(c2[i]>c1[i]) ans = ans*6*(c2[i]-c1[i]-1); 92 */ 93 ans = ans*((c2[i]-c1[i]+1)*(c2[i]-c1[i]+1)*(c2[i]-c1[i]+1)-2*(c2[i]-c1[i])*(c2[i]-c1[i])*(c2[i]-c1[i])+E*E*E); 94 } 95 printf("%lld\n",ans); 96 } 97 return 0; 98 }
标签:
原文地址:http://www.cnblogs.com/shanyr/p/5672302.html