zoukankan      html  css  js  c++  java
  • POJ 2891

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

    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

    Hint

    All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

    模不互素不能用CRT,所有就有了我解线性方程组的方法?

    但是有个关键步骤不太懂。。只是会用这个板子。。

     1 #include <cstdlib>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include<iostream>
     6 #include <cmath>
     7 #include<string>
     8 #define ll long long 
     9 #define dscan(a) scanf("%d",&a)
    10 #define mem(a,b) memset(a,b,sizeof a)
    11 using namespace std;
    12 #define MAXL 1105
    13 #define Endl endl
    14 #define maxn 1000005
    15 ll x,y;
    16 inline ll read()
    17 {
    18     ll x=0,f=1;char ch=getchar();
    19     while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    20     while(ch>='0'&&ch<='9') {x=10*x+ch-'0';ch=getchar();}
    21     return x*f;
    22 }
    23 ll exgcd(ll a,ll b,ll &x,ll &y){
    24     if(b==0) {
    25         x=1;y=0;return a;
    26     }
    27     ll d=exgcd(b,a%b,x,y);
    28     ll temp=x;
    29     x=y;
    30     y=temp-a/b*y;
    31     return d;
    32 }
    33 int main()
    34 {
    35     ll k,a,b,c,d;
    36     while(~scanf("%lld",&k))
    37     {
    38         cin>>a>>b;
    39         //cout<<"hhh"<<Endl;
    40         int flag=0;
    41         for(ll i=1;i<k;++i)
    42         {
    43             c=read();d=read();
    44             if(flag) continue;
    45             ll r=d-b;
    46             ll hhh=exgcd(a,c,x,y);
    47             if(r%hhh) {flag=1;continue;}
    48             ll tmp=c/hhh;
    49             x=((r/hhh*x)%tmp+tmp)%tmp;//不理解
    50             b=a*x+b;
    51             a=a*c/hhh;
    52         }
    53         if(flag) cout<<"-1"<<endl;
    54         else cout<<b<<endl;
    55     }
    56 }
    View Code
  • 相关阅读:
    .net core ef动态orderby
    .Net Core 2.*+ InfluxDB+Grafana+App Metrics实时性能监控
    .net core 第三方工具包集合
    .net core appsetting/获取配置文件
    appium处理混合APP_获取上下文(切换句柄)
    Linux平时常用命令_查看进程_监控日志等命令
    手机monkey测试BUG重现及解决方法
    postman常用公共函数
    Jmeter远程运行_多机联合负载
    httpclient向浏览器发送get和post请求
  • 原文地址:https://www.cnblogs.com/TYH-TYH/p/9378510.html
Copyright © 2011-2022 走看看