Happy 2004(一元线性同余方程组)

题意:本题求解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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值