hdu 3641 2010 Asia Regional Hangzhou Site —— Online Contest 数论题,分解质因子

先记录所有数相乘的质因子,再二分一个x判断x!是否能被M整除

View Code
  1 #include<stdio.h>
2 #include<string.h>
3 #include<math.h>
4 #define lld __int64
5 lld num[110];
6 lld a[110],b[110];
7 void cal(lld n,lld count)
8 {
9 int i,cnt;
10 for(i=2;i*i<=n;i++)
11 {
12 cnt=0;
13 if(n%i==0)
14 {
15 n/=i;
16 cnt++;
17 while(n%i==0)
18 {
19 n/=i;
20 cnt++;
21 }
22 num[i]+=cnt*count;
23 }
24 if(n==1) break;
25 }
26 if(n>1) num[n]+=count;
27 }
28 lld solve(lld n,lld x)
29 {
30 lld ans=0;
31 while(n)
32 {
33 n/=x;
34 ans+=n;
35 }
36 return ans;
37 }
38 bool judge(lld x)
39 {
40 lld i,temp;
41 for(i=1;i<=100;i++)
42 {
43 if(num[i])
44 {
45 temp=solve(x,i);
46 if(num[i]>temp)
47 return false;
48 }
49 }
50 return true;
51 }
52 int main()
53 {
54 int t,n,i;
55 lld ans;
56 scanf("%d",&t);
57 while(t--)
58 {
59 scanf("%d",&n);
60 memset(num,0,sizeof(num));
61 for(i=0;i<n;i++)
62 {
63 scanf("%I64d%I64d",&a[i],&b[i]);
64 cal(a[i],b[i]);
65 }
66 lld left=0,right=(lld(1)<<62);
67 while(left<=right)
68 {
69 lld mid=(left+right)>>1;
70 if(judge(mid))
71 {
72 right=mid-1;
73 ans=mid;
74 }
75 else left=mid+1;
76 }
77 printf("%I64d\n",ans);
78 }
79 return 0;
80 }
81 int t,n,i;
82 lld ans;
83 scanf("%d",&t);
84 while(t--)
85 {
86 scanf("%d",&n);
87 memset(num,0,sizeof(num));
88 for(i=0;i<n;i++)
89 {
90 scanf("%I64d%I64d",&a[i],&b[i]);
91 cal(a[i],b[i]);
92 }
93 lld left=0,right=(lld(1)<<62);
94 while(left<=right)
95 {
96 lld mid=(left+right)>>1;
97 if(judge(mid))
98 {
99 right=mid-1;
100 ans=mid;
101 }
102 else left=mid+1;
103 }
104 printf("%I64d\n",ans);
105 }
106 return 0;
107 }



你可能感兴趣的:(online)