POJ2891&&HDU3579&&HDU1573

38 篇文章 0 订阅

这三道题都是用扩展欧几里得求解同余方程的模板题.

对于同余方程组:

      x=a1 mod m1);   1

      x=a2 mod m2);    2

      方程组有一个小于mm1m2的最小公倍数)的非负整数解的充分必要条件是(a1-a2%GCD(m1m2==0 ,利用扩展欧几里德算法。

      两式联立:a1+m1*y=a2+m2*z

      则:a1-a2=m2*z-m1*y; 这样就可以了解出zy,则:x=a2+m2*z;  

      现在我们将其推广到一般情形:(设m1,m2,···,mk两两互素)

      x=a1mod m1;

     x=a2mod m2;

      ···

      x=akmod mk;其在M=m1*m2*···*mk;中有唯一整数解。

      记Mi=M/mi;因为(Mi,mi=1,故有两整数pi,qi满足Mi*pi+mi*qi=1,如果记ei=Mi*pi;那么:ei=0 mod mj,j!=i; ei=1mod mj,j=i;

      很明显,e1*a1+e2*a2+···+ek*ak就是方程的一个解,加减M倍后就可以得到最小非负整数解了。

   这种情况下 :


   a=B[1](mod W[1])   

   a=B[2](mod W[2])   

   ........   

   a=B[n](mod W[n])   

    

   W[i]W[j] 互质。


如果m1,m2,···,mk不互素,那只能两个两个求了。

      x=a1 mod m1);  

      x=a2 mod m2);   

      解完后,a=x; m=m1m2的最小公倍数。即可



Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 9300 Accepted: 2810

Description

Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ ik) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

Since Elina is new to programming, this problem is too difficult for her. Can you help her?

Input

The input contains multiple test cases. Each test cases consists of some lines.

  • Line 1: Contains the integer k.
  • Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ ik).

Output

Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

Sample Input

2
8 7
11 9

Sample Output

31
这就是两两不互质的情况

实际上就是:

  • x=r1 (mod a1)   
  • x=r2 (mod a2) 
  • ---> //a1*x+a2*y=gcd(a1,a2)=d 
  • ---> //r1+a1*x=r2+a2*y  ---> a1*x+a2*y=r2-r1
  • 然后每次把a更新成LCM(a1,a2)即可
  • 总而言之就是每次只有两个同余方程组,前面求解的答案作为一个整体,与后一个进行计算


    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    long long ex_gcd(long long a,long long b,long long &x,long long &y)
    {
        if(b==0)
        {
            x=1;
            y=0;
            return a;
        }
        long long  tmp,res;
        res=ex_gcd(b,a%b,x,y);
        tmp=x;
        x=y;
        y=tmp-(a/b)*y;
        return res;
    }
    
    int main()
    {
        long long n,a1,a2,r1,r2,i,j,t,g;
        long long x,y;
        bool flag;
        while(cin>>n)
        {
            cin>>a1>>r1;
    
            flag=1;
            for(i=1;i<n;i++)
            {
                cin>>a2>>r2;
                if(flag==0)
                    continue;
                if(a2<=r2)//余数比除数大肯定不可能
                    flag=0;
                g=ex_gcd(a1,a2,x,y);
                if((r2-r1)%g!=0)//不整除则不可能有整数解
                    flag=0;
                else
                {
                    t=a2/g;
                    x=x*(r2-r1)/g;
                    x=(x%t+t)%t;//ax+by=c的最小正整数解
                    r1=a1*x+r1;//更新X
                    a1=a1*a2/g;//更新A,记住一定得两两更新。。。因为它们不一定两两互质啊- -
    
    
                }
            }
            if(flag==0)
            {
                cout<<-1<<endl;
            }
            else
            {
                cout<<r1<<endl;
            }
        }
        return 0;
    }
    


    Hello Kiki

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1860    Accepted Submission(s): 660


    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
     
    与上一题如出一辙。。。

    只不过两个数组更好。。。


    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int t;
    int n;
    int a[15];
    int b[15];
    
    
    int ex_gcd(int a,int b,int &x,int &y)
    {
        if(b==0)
        {
            x=1;
            y=0;
            return a;
        }
        int t,res;
        res=ex_gcd(b,a%b,x,y);
        t=x;
        x=y;
        y=t-(a/b)*y;
        return res;
    }
    
    int shengyu()
    {
        int flag,x,y,gcd,a1,a2,b1,b2,i,t;
        flag=0;
        a1=a[0],b1=b[0];
        for(i=1;i<n;i++)
        {
            a2=a[i];
            b2=b[i];
            gcd=ex_gcd(a1,a2,x,y);
            if((b2-b1)%gcd!=0)
            {
                flag=1;
                break;
            }
            t=a2/gcd;
            x=(x*(b2-b1))/gcd;
            x=(x%t+t)%t;
            b1=a1*x+b1;
            a1=(a1*a2)/gcd;
            b1=(b1%a1+a1)%a1;
        }
        if(flag==1)
        {
            b1=-1;
        }
        if(b1==0&&n>1)
            b1=a1;
        if(b1==0&&n==1)
            b1=a[0];
        return b1;
    }
    int main()
    {
        cin>>t;
        int cas=0;
        while(t--)
        {
            cin>>n;
            for(int i=0;i<n;i++)
            {
                cin>>a[i];
            }
            for(int i=0;i<n;i++)
            {
                cin>>b[i];
            }
             printf("Case %d: %d\n",++cas,shengyu());
        }
        return 0;
    }
    
    

    X问题

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2864    Accepted Submission(s): 909


    Problem Description
    求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … (0 < a[i] <= 10)。
     

    Input
    输入数据的第一行为一个正整数T,表示有T组测试数据。每组测试数据的第一行为两个正整数N,M (0 < N <= 1000,000,000 , 0 < M <= 10),表示X小于等于N,数组a和b中各有M个元素。接下来两行,每行各有M个正整数,分别为a和b中的元素。
     

    Output
    对应每一组输入,在独立一行中输出一个正整数,表示满足条件的X的个数。
     

    Sample Input
       
       
    3 10 3 1 2 3 0 1 2 100 7 3 4 5 6 7 8 9 1 2 3 4 5 6 7 10000 10 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9
     

    Sample Output
       
       
    1 0 3
    这道题是求1-N满足条件的同余方程的解的个数。。。

    N太大暴力搜完全不科学。。但是由于条件很容易知道其实1-N可以缩小为1-LCM(a1,a2)。然后你再这里面进行搜索就很简单了。。

    在1-LCM中如果有一个解, 然后再看N%LCM是否大于改解所对应的值因为如果大则答案还得+1,否则答案就是N/ANS

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int a[15];
    int b[15];
    
    int gcd(int a,int b)
    {
        if(b==0)
            return a;
            else
                return gcd(b,a%b);
    }
    
    int main()
    {
        int t,n,m,k;
    
        cin>>t;
    
        while(t--)
        {
    
            cin>>n>>m;
    
            for(int i=0;i<m;i++)
            {
                cin>>a[i];
            }
            for(int i=0;i<m;i++)
            {
                cin>>b[i];
            }
    
            int i,j;
            int tmp=1;
    
            long long ans=1;
            for(i=0;i<m;i++)
            {
                tmp=gcd(ans,a[i]);
                ans=ans*a[i]/tmp;
            }
           // cout<<ans<<endl;
    
            k=0;
    
            for( i=1;i<=ans&&i<=n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(i%a[j]!=b[j])
    
                        break;
    
                }
                if(j==m)
                {
                    k=i;
                    break;
                }
            }
            if(k==0)
            {
                cout<<0<<endl;
            }
    
            else
            {
                int yu=n%ans;
                if(yu>=k)k=n/ans+1;
                else k=n/ans;
                printf("%d\n",k);
            }
        }
    
        return 0;
    
    }
    
    
    目测前两题可以当做板子使用。。。接下来应该会继续刷利用扩展欧几里得求逆元的题吧。。。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值