中国剩余定理andHDUOJ3579

求解一次同余式组的方法

一、除数互质

//中国剩余定理 ,r[]存放余数 ,prime[]存放两两互质的数(除数)  
int Chinese_Remainder(int r[],int prime[],int len)  {  
    int i,d,x,y,m,n=1,sum=0;  
    //计算所以除数的积n,也是所以除数的最小公倍数  
    for(i=0;i<len;i++)  
        n*=prime[i];  
    //计算符合所有条件的数  
    for(i=0;i<len;i++)  {  
        m=n/prime[i];//计算除去本身的所有除数的积m  
      d=exgcd(prime[i],m,x,y);//计算prime[i]*x+m*y=gcd(prime[i],m)的一个解y  
        //累加整数解y的同并不断对n取余,其利用公式:(a+b)%c=(a%c+b%c)%c  
        sum=(sum+y*m*r[i])%n;    }  
    return (n+sum%n)%n;//满足所以方程的最小解  }  

二、除数不互质

ll a1 = a[1],b1 = b[1];//a[]是除数,b[]是余数
        bool flag = 1;
        for(int i = 2; i <= n; i++)
        {
            ll A = a1, B = a[i], C = b[i] - b1, x, y;
            ll gcd = exgcd(A, B, x, y);
            if(C % gcd)  
            {
                flag = 0;
                break; 
            }   
            x = ((x * C / gcd) % (B / gcd) +( B / gcd)) % (B / gcd);
            b1 = a1 * x + b1;
            a1 = a1 / gcd * a[i];
        }
        if(!flag) 
            printf("Case %d: -1\n", no);//无解
        else
        {
            //a1x+b1=P;(a1>=0,P求的是最小正整数)
            //b1!=0 --> x=0,b1=P
            //b1==0 --> x=1,a1=P
            if(b1) 
                 printf(" %I64d\n", b1);
            else 
                printf(" %I64d\n", a1); 
        }

HDUOJ3579Hello Kiki

Problem Description
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一群鸭,快来快来 数一数,二四六七八". And then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.
 

Input
The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi
 

Output
For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.
 

Sample Input
 
 
2 2 14 57 5 56 5 19 54 40 24 80 11 2 36 20 76
 

Sample Output
 
 
Case 1: 341 Case 2: 5996

题意:

给一组除数a[],一组余数b[]求满足每个ans%a[i]==b[i]的最小的ans

#include <cstdio>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define PI         acos(-1)
#define inf        0x3f3f3f3f
#define EPS        1e-6
#define mem(a, b)  memset(a, b, sizeof(a))
#define ll long long
#define N 5005
#define mian main

ll exgcd (ll a, ll b, ll &x, ll &y)
{
   if(b == 0)
   {
       x = 1;
       y = 0;
       return a;
   }
   ll re = exgcd(b, a % b, x, y);
   ll tmp = x;
   x = y;
   y = tmp - a / b * y;
   return re;
}

int main()
{
    int n, T;
    scanf("%d", &T);
    ll a[N], b[N];
    int no = 0;
    while(T--)
    {
        no++;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%I64d", &a[i]);
        for(int i = 1;i <= n; i++)
            scanf("%I64d", &b[i]);
        ll a1 = a[1],b1 = b[1];
        bool flag = 1;
        for(int i = 2; i <= n; i++)
        {
            ll A = a1, B = a[i], C = b[i] - b1, x, y;
            ll gcd = exgcd(A, B, x, y);
            if(C % gcd) 
            {
                flag = 0;
                break;
            }   
            x = ((x * C / gcd) % (B / gcd) +( B / gcd)) % (B / gcd);
            b1 = a1 * x + b1;
            a1 = a1 / gcd * a[i];
        }
        if(!flag) 
            printf("Case %d: -1\n", no);
        else{
            if(b1) 
                printf("Case %d: %I64d\n", no, b1);
            else 
                printf("Case %d: %I64d\n", no, a1);
        }
}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值