【HDU - 1452】 Happy 2004(因子和,逆元,快速幂)
題干:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Happy 2004
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1863????Accepted Submission(s): 1361
?
Problem Description
Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your job is to determine S modulo 29 (the rest of the division of S by 29).
Take X = 1 for an example. The positive integer divisors of 2004^1 are 1, 2, 3, 4, 6, 12, 167, 334, 501, 668, 1002 and 2004. Therefore S = 4704 and S modulo 29 is equal to 6.
?
?
Input
The input consists of several test cases. Each test case contains a line with the integer X (1 <= X <= 10000000).?
A test case of X = 0 indicates the end of input, and should not be processed.
?
?
Output
For each test case, in a separate line, please output the result of S modulo 29.
?
?
Sample Input
?1 10000 0
?
?
Sample Output
?6 10
?
?
Source
ACM暑期集訓隊練習賽(六)
?
題意:求2004^x的所有因子和對29取余的結果。
?
題解:因為所有數都可以被分解成a=p1^c1*p2^c2*...*pk^ck,并有約數和定理:sum=(p1^0+...+p1^c1)*(p2^0+...+p2^c1)*...*(pk^0+...+pk^ck)。每一項可以通過等比數列求和得到。2004=2^2*3*167,所以答案就是2^(2*n+1)*3^(n+1)*167^(n+1)/(2*166)。由于需要取余,所以不能直接除要求出2*166的逆元改為相乘。
?
AC代碼:
#include<bits/stdc++.h> #define debug cout<<"aaa"<<endl #define mem(a,b) memset(a,b,sizeof(a)) #define LL long long #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define MIN_INT (-2147483647-1) #define MAX_INT 2147483647 #define MAX_LL 9223372036854775807i64 #define MIN_LL (-9223372036854775807i64-1) using namespace std;const int N = 100000 + 5; const int mod = 29;int e_gcd(int a,int b,int &x,int &y) {if(b==0) {x=1,y=0;return a;}int q=e_gcd(b,a%b,y,x);y-=a/b*x;return q;}int quick(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a)%mod;}b>>=1;a=(a*a)%mod;}return ans; }int main(){int n,x,y,a,b,c,ans;exgcd(166*2,29,x,y);while(~scanf("%d",&n)&&n){a=(quick(2,2*n+1)-1)%mod;b=(quick(3,n+1)-1)%mod;c=(quick(167,n+1)-1)%mod;ans=((a*b*c*x)%mod+mod)%mod;printf("%d\n",ans);}return 0; }?
總結
以上是生活随笔為你收集整理的【HDU - 1452】 Happy 2004(因子和,逆元,快速幂)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: quickdcf.exe - quick
- 下一篇: 【CodeForces - 357D】X