poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 11970 | Accepted: 3788 |
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 题目大意: x % ai = ri 求满足条件的最小的x 刚开始看中国剩余定理,直接套用中国剩余定理模板,结果各种RE,原来还有不是两两互质的情况,还是so young 啊!!!! 那么应该怎么处理这种情况呢, 合并方程求解
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h> using namespace std; const int N = ;
typedef __int64 ll;
ll r, n[N], b[N]; void gcd(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
r = a;
return ;
}
gcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - a / b * y;
} ll CRT2(ll n[], ll b[], ll m)
{
int f = ;
ll n1 = n[], n2, b1 = b[], b2, c, t, k, x, y;
for(ll i = ; i < m ; i++)
{
n2 = n[i];
b2 = b[i];
c = b2 - b1;
gcd(n1, n2, x, y);//扩展欧几里德
if(c % r != )//无解
{
f = ;
break;
}
k = c / r * x;//扩展欧几里德求得k
t = n2 / r;
k = (k % t + t) % t;
b1 = b1 + n1 * k;
n1 = n1 * t;
}
if(f == )
return -;
return b1;
} int main()
{
ll k;
while(~scanf("%I64d", &k))
{
for(ll i = ; i < k ; i++)
scanf("%I64d%I64d", &n[i], &b[i]);
printf("%I64d\n", CRT2(n, b, k));
}
return ;
}
poj 2981 Strange Way to Express Integers (中国剩余定理不互质)的更多相关文章
- 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 ...
- POJ 2981 Strange Way to Express Integers 模线性方程组
http://poj.org/problem?id=2891 结果看了半天还是没懂那个模的含义...懂了我再补充... 其他的思路都在注释里 /********************* Templa ...
- 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 ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
随机推荐
- python学习---python基础一
一.Python介绍 1.python出生与应用 python的创始人是吉多.范罗苏姆(龟叔).1989年圣诞在家闲着无聊,决心开发一个新的脚本解释程序,作为ABC语言的一种继承 python崇尚的是 ...
- 部分流媒体协议及流媒体开发框架vitamio
流媒体协议部分RTP.RTCP.RTSP.MMS.HLS.HTTP progressive streaming 流媒体协议:(RTP.RTCP.RTSP.MMS.HLS.HTTP progress ...
- uwsgi的python2+3多版本共存实操使用virtualenv
1首先,机器需要有python2和python3的可执行环境.确保pip和pip3命令可用.原理就是在哪个环境下安装uwsgi.uwsgi启动的时候,就用的哪个python版本 2安装virtuale ...
- vb.net 与 c# 运算符区别
vb.net vs c# 详细的Operators运算符区别 vb.net ===================== Comparison = < > <= >= <& ...
- 决策树与树集成模型(bootstrap, 决策树(信息熵,信息增益, 信息增益率, 基尼系数),回归树, Bagging, 随机森林, Boosting, Adaboost, GBDT, XGboost)
1.bootstrap 在原始数据的范围内作有放回的再抽样M个, 样本容量仍为n,原始数据中每个观察单位每次被抽到的概率相等, 为1/n , 所得样本称为Bootstrap样本.于是可得到参数θ的 ...
- 跟我学算法- tensorflow 卷积神经网络训练验证码
使用captcha.image.Image 生成随机验证码,随机生成的验证码为0到9的数字,验证码有4位数字组成,这是一个自己生成验证码,自己不断训练的模型 使用三层卷积层,三层池化层,二层全连接层来 ...
- Linux 移除python Error: Trying to remove “yum”, which is protected
>yum intall python >yum -y remove python 出现Error: Trying to remove "yum", which is p ...
- ubuntu安装rtx
终端安装RTX sudo apt-get install wine 安装wine下的window扩展包安装工具winetricks,在终端下输入: sudo wget http://winetrick ...
- eclipse怎么自定义工具栏
1.点击透视图按钮---->右键---->Customize: 2.勾选或者去掉相关项目:
- The Process of Google Hiring
[The Process of Google Hiring] 1.keynote 1: The Google hiring process is designed to hire the most t ...