Strange Way to Express Integers

Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u

 

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.

题意:给你k组数。x%M[i]=A[i];

思路:中国剩余定理,扩展欧几里德

不会的可以参考:http://blog.csdn.net/u010579068/article/details/45422941

转载请注明出处:寻找&星空の孩子

题目链接:http://poj.org/problem?id=2891

#include<stdio.h>
#define LL __int64 void exgcd(LL a,LL b,LL& d,LL& x,LL& y)
{
if(!b){d=a;x=;y=;}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
LL gcd(LL a,LL b)
{
if(!b){return a;}
gcd(b,a%b);
} LL M[],A[]; LL China(int r)
{
LL dm,i,a,b,x,y,d;
LL c,c1,c2;
a=M[];
c1=A[];
for(i=; i<r; i++)
{
b=M[i];
c2=A[i];
exgcd(a,b,d,x,y);
c=c2-c1;
if(c%d) return -;//c一定是d的倍数,如果不是,则,肯定无解
dm=b/d;
x=((x*(c/d))%dm+dm)%dm;//保证x为最小正数//c/dm是余数,系数扩大余数被
c1=a*x+c1;
a=a*dm;
}
if(c1==)//余数为0,说明M[]是等比数列。且余数都为0
{
c1=;
for(i=;i<r;i++)
c1=c1*M[i]/gcd(c1,M[i]);
}
return c1;
}
int main()
{
int n; while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
{
scanf("%I64d%I64d",&M[i],&A[i]);
}
if(n==){ printf("%I64d\n",A[]);continue;}
LL ans=China(n);
printf("%I64d\n",ans); }
return ;
}

Strange Way to Express Integers(中国剩余定理+不互质)的更多相关文章

  1. poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

  2. POJ 2891 Strange Way to Express Integers 中国剩余定理解法

    一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...

  3. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  4. POJ 2891 Strange Way to Express Integers(中国剩余定理)

    题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...

  5. POJ2891 Strange Way to Express Integers [中国剩余定理]

    不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring&g ...

  6. POJ 1006 Biorhythms --中国剩余定理(互质的)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103539   Accepted: 32012 Des ...

  7. Hello Kiki(中国剩余定理——不互质的情况)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)

    分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...

  9. X问题(中国剩余定理+不互质版应用)hdu1573

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. java安全与加解密

    1 安全 安全性相关内容分为认证.授权和审计(发现安全问题时可以查看相关历史记录) 用户认证 java API表示主体的是javax.security.auth.Subject类型,表示用户身份标识的 ...

  2. python持久化

    将对象转为二进制字节流(pickle) import pickle my_list = [1,2,3] pickle_file = open('my_list.pkl', 'wb') #注意二进制写入 ...

  3. Docker集群管理工具 - Kubernetes 部署记录 (运维小结)

    一.  Kubernetes 介绍 Kubernetes是一个全新的基于容器技术的分布式架构领先方案, 它是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,Kubernete ...

  4. Container Adaptors

    Notes from C++ Primer stack and queue: based on deque priority_queue:    based on vector Standard li ...

  5. fscanf_s与scanf_s的宽度参数与缓冲区参数分析

    fscanf_s函数 在文件操作中经常会用到fscanf这个函数,但是在VC和VS中会有警告 意思是编译器觉得fscanf不安全,叫你考虑用一下fscanf_s这个函数来代替fscanf,fscanf ...

  6. Java 代码需要使用转义符的地方

    1.正则表达式特殊字符 Java 代码中使用到正则表达式里的特殊字符需要使用转义符 \ 进行转义 . ? * + ! ^ $ [ ] ( ) \ 因为反斜线 \ 也是特殊字符,所以转义需双反斜线 \\ ...

  7. C语言中volatile的作用和使用方法

    在程序设计中,尤其是在C语言.C++.C#和Java语言中,使用volatile关键字声明的变量或对象通常具有与优化.多线程相关的特殊属性. 通常,volatile关键字用来阻止(伪)编译器认为的无法 ...

  8. PHP画矩形,椭圆,圆,画椭圆弧 ,饼状图

    1:画矩形: imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col ) imagere ...

  9. LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium

    题目: Evaluate Reverse Polish Notation Evaluatethe value of an arithmetic expression in Reverse Polish ...

  10. Spring Boot 配置随机数技巧

    Spring Boot支持在系统加载的时候配置随机数. 添加config/random.properties文件,添加以下内容: #随机32位MD5字符串 user.random.secret=${r ...