hdu1452 Happy 2004 x^y的因子和 逆元 快速乘法

xiaoxiao2021-02-28  115

http://acm.hdu.edu.cn/showproblem.php?pid=1452

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的逆元改为相乘。

代码:

#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 exgcd(int a,int b,int &x,int &y){//扩展欧几里德求逆元 if(b==0){ x=1; y=0; return a; } int r=exgcd(b,a%b,x,y); int t=x; x=y; y=t-a/b*y; return r; } 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; }

转载请注明原文地址: https://www.6miu.com/read-84107.html

最新回复(0)