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 }
  • 相关阅读:
    html5标签---不常用新标签的整理
    拖拽demo--兼容--全局捕获
    Linux now!--网络配置
    windows下 memcached 和 redis 服务器安装
    MySQL5.6安装步骤(windows7/8_64位)
    zend 环境
    mysql自增id超大问题查询
    烦人的运营后台导出大批量数据
    kafka环境搭建和使用(python API)
    分布式系统中zookeeper实现配置管理+集群管理
  • 原文地址:https://www.cnblogs.com/Enumz/p/4063499.html
Copyright © 2011-2022 走看看