zoukankan      html  css  js  c++  java
  • POJ 2891 Strange Way to Express Integers

    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.

    Source

    线性同余方程组

    像普通同余方程那样求出解,然后将解的通式带入下一个方程,最后求出的

    有方程:a1*x-a2*y=b2-b1

    设a=a1/t,b=a2/t,c=(b2-b1)/t t=gcd(a,b)

    用exgcd得到a*x+b*y=c的解x0,通解x=x0+k*b,k为整数←目前这个有点理解不了

    带入a1*x+b1=n

    a1*b*k+a1*x0+b1=n

    所以b1=b1+a1*x0,a1=a1*b

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #define LL long long
     7 using namespace std;
     8 int n;
     9 LL gcd(LL a,LL b){
    10     return (!b)?a:gcd(b,a%b);
    11 }
    12 void exgcd(LL a,LL b,LL &x,LL &y){
    13     if(!b){
    14         x=1;y=0;return;
    15     }
    16     exgcd(b,a%b,x,y);
    17     LL t=x;
    18     x=y;
    19     y=t-a/b*y;
    20     return;
    21 }
    22 int main(){
    23     bool flag;
    24     LL a1,a2,b1,b2;
    25     while(scanf("%d",&n)!=EOF){
    26         int i,j;
    27         scanf("%I64d%I64d",&a1,&b1);
    28         LL a,b,c,x,y;
    29         flag=0;
    30         for(i=1;i<n;i++){
    31             scanf("%I64d%I64d",&a2,&b2);
    32             if(flag)continue;
    33             a=a1;
    34             b=a2;
    35             c=b2-b1;
    36             LL tmp=gcd(a,b);
    37             if(c%tmp!=0){//无解
    38                 printf("-1
    ");
    39                 flag=1;
    40             }
    41             else{
    42                 a/=tmp;b/=tmp;c/=tmp;
    43                 exgcd(a,b,x,y);
    44                 x=((c*x)%b+b)%b;//求出最小整数解 
    45                 b1=b1+a1*x;//带入 
    46                 a1=a1*b;// 
    47             }
    48         }
    49         if(!flag)printf("%I64d
    ",b1);
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    边缘检测算法——Canny和LoG边缘检测算法
    Java读书笔记03 输入输出
    CMake入门
    二叉树及二叉树的遍历
    Java读书笔记11 图形程序——颜色 字体 图像
    XNA实现骨骼动画 归纳总结
    Java读书笔记09 内部类
    Java读书笔记02 基础知识
    Java用户界面 模型视图控制器(MVC)模式
    简单理解传值和传引用
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5660802.html
Copyright © 2011-2022 走看看