zoukankan      html  css  js  c++  java
  • POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers

    DescriptionElina 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

    题目大意:

        给定T个(a,b) 求解最小整数 X,使X满足 X mod a = b

    解题思路:

        http://www.cnblogs.com/Enumz/p/4063477.html

    Code:

     1 /*************************************************************************
     2     > File Name: poj2891.cpp
     3     > Author: Enumz
     4     > Mail: 369372123@qq.com
     5     > Created Time: 2014年10月28日 星期二 02时50分07秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<cstdio>
    10 #include<cstdlib>
    11 #include<string>
    12 #include<cstring>
    13 #include<list>
    14 #include<queue>
    15 #include<stack>
    16 #include<map>
    17 #include<set>
    18 #include<algorithm>
    19 #include<cmath>
    20 #include<bitset>
    21 #include<climits>
    22 #define MAXN 100000
    23 #define LL long long
    24 using namespace std;
    25 LL extended_gcd(LL a,LL b,LL &x,LL &y) //返回值为gcd(a,b)
    26 {
    27     LL ret,tmp;
    28     if (b==0)
    29     {
    30         x=1,y=0;
    31         return a;
    32     }
    33     ret=extended_gcd(b,a%b,x,y);
    34     tmp=x;
    35     x=y;
    36     y=tmp-a/b*y;
    37     return ret;
    38 }
    39 int main()
    40 {
    41     LL N;
    42     while (cin>>N)
    43     {
    44         long long a1,m1;
    45         long long a2,m2;
    46         cin>>a1>>m1;
    47         if (N==1)
    48             printf("%lld
    ",m1);
    49         else
    50         {
    51             bool flag=0;
    52             for (int i=2;i<=N;i++)
    53             {
    54                 cin>>a2>>m2;
    55                 if (flag==1) continue;
    56                 long long x,y;
    57                 LL ret=extended_gcd(a1,a2,x,y);
    58                 if ((m2-m1)%ret!=0)
    59                     flag=1;
    60                 else
    61                 {
    62                     long long ans1=(m2-m1)/ret*x;
    63                     ans1=ans1%(a2/ret);
    64                     if (ans1<0) ans1+=(a2/ret);
    65                     m1=ans1*a1+m1;
    66                     a1=a1*a2/ret;
    67                 }
    68             }
    69             if (!flag)
    70                 cout<<m1<<endl;
    71             else
    72                 cout<<-1<<endl;
    73         }
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    作男人 一定要有品位
    如何管理“人”
    Facebook怎样开发软件:工程师驱动的文化(转)
    为人处事100条——修身养性,经典收藏!
    抽空看看这些电影
    曹重英:技术人员也要打造人脉竞争力(转)
    动态分段统计SQL
    不成熟男人与成熟男人的区别
    Ubuntu11.10国内更新源地址汇总以及添加方法(目前最全最快的源)
    ubuntu11.10 64bits机器安装flash方法
  • 原文地址:https://www.cnblogs.com/Enumz/p/4063499.html
Copyright © 2011-2022 走看看