Strange Way to Express Integers(中国剩余定理+不互质)
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 ≤ 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
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(中国剩余定理+不互质)的更多相关文章
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- POJ 2891 Strange Way to Express Integers(中国剩余定理)
题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...
- POJ2891 Strange Way to Express Integers [中国剩余定理]
不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring&g ...
- POJ 1006 Biorhythms --中国剩余定理(互质的)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103539 Accepted: 32012 Des ...
- Hello Kiki(中国剩余定理——不互质的情况)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- X问题(中国剩余定理+不互质版应用)hdu1573
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- 准备在electron上用vue,结果卡在了sqlite3
vue.js看书有一段时间了,也准备动手做一个electron的程序.目录似乎有两种方式搭建方式 一种是使用vue.cli构建工具,就是所谓的脚手架,分别安装vue和electron https:// ...
- HTML之<meta>使用和说明
关于<meta>,我们都不陌生,随意打开一个网页查看源代码就可以看到<head>里出现它的身影. 简单来说,<meta>是描述 HTML 文档的元数据.例如网页描述 ...
- PHP字符串函数之 sscanf echo print sprintf vsprintf printf vprintf fprintf vfprintf
sscanf – 根据指定格式解析输入的字符 echo – 输出一个或多个字符串 print – 输出字符串 sprintf – 返回格式化字符串 vsprintf – 返回格式化字符串 (参数为数组 ...
- Flask中的before_request after_request
1.@app.before_request 在请求(request)之前做出响应 @app.before_request 也是一个装饰器,他所装饰的函数,都会在请求进入视图函数之前执行 2.@app. ...
- fiddler电脑抓包和手机抓包
概述 以前听别人说抓包抓包的,听起来很神秘高大上的样子,想入门又不知道从何学起.今天偶然在工作中遇到了以下2个需求: 改线上的代码,特别是PC端js代码. 写了一个移动端页面,由于跨域,改了host地 ...
- Docker优势
设计,开发 ---> 测试 ----> 部署,运行 代码+运行环境 ---> 镜像 image 环境一致,资源占用少 自动化平台 Docker image的制作很重要
- API接口设计:防参数篡改+防二次请求
API接口由于需要供第三方服务调用,所以必须暴露到外网,并提供了具体请求地址和请求参数 为了防止被第别有用心之人获取到真实请求参数后再次发起请求获取信息,需要采取很多安全机制 1.首先: 需要采用ht ...
- mybatis 详解(一)
http://www.cnblogs.com/ysocean/p/7271600.html 1.什么是MyBatis? MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目 ...
- onload事件
onload事件:页面加载(文本和图片)完毕的时候, onload的作用: JS加载时和html是同步加载的,如果使用元素在定义元素之前易报错: <!DOCTYPE html> <h ...
- vue elementui 引入第三方icon iconfront
elementui框架自带icon在开发大型前端应用时显得捉襟见肘.淘宝开源的iconfront的图标库上有很多优秀的icon图标.elementui支持整合iconfront到应用中,步骤如下: 首 ...