light oj 1236 - Pairs Forming LCM & uva 12546 - LCM Pair Sum
第一题给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,……em,
则结果为((1+2*e1)*(1+2*e2)……(1+2*em)+1)/2.
代码如下:
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <iostream> 5 #define M 10000005 6 #define mod 1000000007 7 #define ll unsigned long long 8 using namespace std; 9 ll prime[M/5],cnt; 10 bool f[M]; 11 void init() 12 { 13 int i,j; 14 cnt=0; 15 for(i=2;i<M;i++){ 16 if(!f[i]) prime[cnt++]=i; 17 for(j=0;j<cnt&&i*prime[j]<M;j++){ 18 f[i*prime[j]]=1; 19 if(i%prime[j]==0) break; 20 } 21 } 22 } 23 ll solve(ll n) 24 { 25 ll ans=1; 26 for(ll i=0;i<cnt&&(ll)prime[i]*prime[i]<=n;i++){ 27 if(n%prime[i]==0){ 28 ll t=1; 29 n/=prime[i]; 30 while(n%prime[i]==0){ 31 t++; 32 n/=prime[i]; 33 } 34 ans*=(ll)(1+t*2); 35 } 36 } 37 if(n>1) ans=3*ans; 38 return (ans+1)/2; 39 } 40 int main() 41 { 42 int t,ca=0; 43 ll n; 44 init(); 45 scanf("%d",&t); 46 while(t--){ 47 scanf("%llu",&n); 48 printf("Case %d: %llu\n",++ca,solve(n)); 49 } 50 return 0; 51 }
第二题给定一个数N,其质因子pi及个数ei给定,
则结果为(1+p1+p1^2+……+p1^ei+ei*p1^ei)*……*(1+pm+pm^2+……+pm^em+em*pm^em)+N.
代码如下:
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <iostream> 5 #define mod 1000000007 6 #define ll long long 7 using namespace std; 8 ll Pow(ll a,int b) 9 { 10 ll ans=1; 11 while(b){ 12 if(b&1) ans=(ans*a)%mod; 13 b>>=1; 14 a=(a*a)%mod; 15 } 16 return ans; 17 } 18 ll sum(ll a,int b) 19 { 20 if(b==0) return 1; 21 if(b&1) return (1+Pow(a,(b+1)/2))*sum(a,b/2)%mod; 22 else return ((1+Pow(a,b/2+1))*sum(a,b/2-1)%mod+Pow(a,b/2))%mod; 23 } 24 int main() 25 { 26 int t,ca=0,n,p,e; 27 scanf("%d",&t); 28 while(t--){ 29 scanf("%d",&n); 30 ll ans=1,xx,num=1; 31 for(int i=0;i<n;i++){ 32 scanf("%d%d",&p,&e); 33 xx=Pow(p,e); 34 num=(num*xx)%mod; 35 xx=(xx*e)%mod; 36 xx=(xx+sum(p,e))%mod; 37 ans=(ans*xx)%mod; 38 } 39 printf("Case %d: %lld\n",++ca,(ans+num)%mod); 40 } 41 return 0; 42 }