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 ...
随机推荐
- 深入浅出 Java Concurrency (5): 原子操作 part 4 CAS操作
在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁(后面的章节还会谈到锁). 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度 ...
- urllib2模块的基本使用(四)
urllib2库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urllib2. urllib2 是 P ...
- vue轮播(完整详细版)
轮播组件vue <swiper :options="swiperOption" class='swiper-box'> <swiper-slide v-f ...
- myeclipse通过数据表生成jpa或hibernate实体
1. 创建数据库连接 2. 选择表 3. 生成 hibernate mapping 4. 生产jpa
- 最简单的WebService
写在前面的话: 当两个人碰面后,产生了好感,如果需要得到双方的信息,那么双方的交流是必不可少的!应用程序也如此, 各个应用程序之间的交流就需要WebService来作为相互交流的桥梁! 项目目的: 程 ...
- 10-SSH综合案例:前台用户模块:邮箱服务器配置
之前发邮件是自己发到服务器还是?是自己搭建一个还是注册一个163啊?自己可以搭建一个邮箱的服务器然后去发送邮件.就是你必须得在这上面有了账户才能发,你也可以往网络上发.你的主机就是一台邮箱服务器了.你 ...
- innodb count优化测试
对于索引优化真的是门课题,先来研究下最平常的问题,innodb引擎下 怎么让count(*)快一点. 首先需要清楚 innodb 默认是对主键建立聚簇索引,如果没有主键,那就是对具有唯一且非空值的索引 ...
- mysql常用的信息查询函数
mysql常用信息函数 select version(); --当前数据库服务器版本信息 select database(); --当前使用的数据库 select current_user() 或 s ...
- Illegal mix of collations for operation 'like' while searching with Ignited-Datatables
Stack Overflow Questions Developer Jobs Tags Users Log In Sign Up Join Stack Overflow to learn, sh ...
- Laravel 在哪些地方使用了 trait ?
laravel 框架大量使用了traits. 简单举几个例子: 在Eloquent中使用了trait .然后在model初始化的时候,有个boot方法,会自动判断当前的类用了哪些trait.然后得到一 ...