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 ...
随机推荐
- Programming | 中/ 英文词频统计(MATLAB实现)
一.英文词频统计 英文词频统计很简单,只需借助split断句,再统计即可. 完整MATLAB代码: function wordcount %思路:中文词频统计涉及到对"词语"的判断 ...
- Latex命令
.tex代码中 | 在pdf文档中 空一行 代表回车,下一行空两格 // 代表回车,下一行顶格
- ScriptOJ-safeGet#99
const safeGet = (data, path) => { if(!path) return undefined; const pathArr = path.split('.'); le ...
- Linux(以RHEL7为例)下添加工作区的方法|| The Way To Add Workspace On Linux
Linux(以RHEL7为例)下添加工作区的方法 The Way To Add Workspace On Linux 作者:钟凤山(子敬叔叔) 编写时间:2017年5月11日星期四 需求:有时候在使用 ...
- 关于SQL\SQL Server的三值逻辑
在SQL刚入门的时候,我们筛选为某列值为NULL的行,一般会采用如下的方式: SELECT * FROM Table AS T WHERE T.Col=NULL 而实际上此种写法无法得到想要的结果.此 ...
- Vue自定义指令,ref ,sync,slot
一.自定义指令 vue中可以自己设置指令,通过directive来实现,有2种创建方式,一种是局部创建,一种是全局创建. 第一种:局部创建 如果想注册局部指令,组件中也接受一个 directives ...
- 在windows如何操控一些屌炸天的linux命令[利刃篇]
作为开发者,也许你会说,不会linux简直弱爆了.是的,但是,倒底有多少人是真正在用linux进行开发工作呢,也许是我孤陋寡闻,但我相信,windows操作很多东西是真方便,不愿意使用linux直接开 ...
- OpenGL ES: iOS 自定义 UIView 响应屏幕旋转
iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...
- Linux下安装配置与使用MySQL数据库
Linux下安装配置与使用MySQL数据库 在Linux下安装做开发时往往少不了要使用到MySQL数据库,下面就Linux系统为例讲解一下,如何安装MySQL数据库,如何启用/停.止MySQL服务,如 ...
- vsftpd3.0.3配置
2019.2.18更新 证实可用!!! 原文: 这两天测试在Ubuntu18.04上搭建一个ftp服务器,搜了一下大家都在用vsftpd,于是根据这个大佬的基础教程搭了一个,搭完一切正常,在windo ...