题意:本题求解2004的n次方的因子和对29取模的值是多少;
分析:
根据基本算数定理,2004为 2^2*3*167,那么2004^n即为 2^(2*n)*3^n*167^n;那么其因子和为(2^(2*n+1)*3^(n+1)*167^(n+1))/2*166;因为是求解模29的值所以要用逆元 2对于29的逆元为15,而166对于29的逆元为18。此外,本题要采用快速幂的方式求解;
代码如下:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <utility>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
void _gcd(int a,int b,int &x,int &y){
if(b==1){
x=1;
y=1-a;
}
else{
int x1,y1;
_gcd(b,a%b,x1,y1);
x=y1;
y=x1-(a/b)*x;
}
}//扩展欧几里得算法
int main(){
int a,b,c,d;
int k=1;
while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){
if(a==-1&&b==-1&&c==-1&&d==-1)break;
int a_1,b_1,c_1;
a_1=23;
b_1=28;
c_1=33;
int cc=b-a;
int x,y;
_gcd(a_1,b_1,x,y);
x=(x*cc%b_1+b_1)%b_1;
x=x*a_1+a;
a=x;
cc=c-x;
a_1=b_1*a_1;
_gcd(a_1,c_1,x,y);
x=x*cc%c_1;
while(x*a_1+a>d){
x-=c_1;
}
while(x*a_1+a<=d){
x+=c_1;
}
x=x*a_1+a;
printf("Case %d: the next triple peak occurs in %d days.\n",k++,x-d);
}
return 0;
}