zoukankan      html  css  js  c++  java
  • poj 2891 模线性方程组求解

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

    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 ≤ i  k) 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 ≤ i  k).

    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
    题目大意:k个模线性方程求解
    AC代码:
    #include <iostream>
    using namespace std;
    
    __int64 Extended_Euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
    {
        __int64 t,d;
        if(b==0)
        {x=1;y=0;return a;}
        d = Extended_Euclid(b,a%b,x,y);
        t = x;
        x=y;
        y=t-(a/b)*y;
        return d;
    }
    
    __int64 CRT(__int64 n)
    {
        __int64 i,x,y;
        __int64 d,c,t,m,b,mm,bb;
        int flag=1;
        scanf("%I64d %I64d",&m,&b);
        mm=m;bb=b;
        if(n==1)
            return b%m;
        for(i=1;i<n;i++)
        {
            scanf("%I64d %I64d",&m,&b);
            if(flag)
            {
                d=Extended_Euclid(mm,m,x,y);
                c=b-bb;
                if(c%d)
                    flag=0;
                else
                {
                    t=m/d;
                    bb=mm*((c/d*x%t+t)%t)+bb;
                    mm=mm*m/d;
                }
            }
        }
        if(flag)
            return bb%mm;
        else
            return -1;
    }
    int  main()
    {
        int n;
        while(cin>>n)
            printf("%I64d
    ",CRT(n));
        return 0;
    }
  • 相关阅读:
    centos 下查找软件安装在哪里的命令
    Ubuntu常用命令大全
    linux下vi命令大全
    查看linux系统版本命令
    Linux系统安装时分区的选择(推荐)
    Java subList的使用
    Java中unicode增补字符(辅助平面)相关用法简介
    Java编码方式再学
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 283. Move Zeroes
  • 原文地址:https://www.cnblogs.com/xiong-/p/3213320.html
Copyright © 2011-2022 走看看