http://poj.org/problem?id=2891

Strange Way to Express Integers
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 a1a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) 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 airi (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 (中国剩余定理不互质)的更多相关文章

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

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

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

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

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

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

  4. POJ 2981 Strange Way to Express Integers 模线性方程组

    http://poj.org/problem?id=2891 结果看了半天还是没懂那个模的含义...懂了我再补充... 其他的思路都在注释里 /********************* Templa ...

  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. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  8. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  9. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

随机推荐

  1. 代理Servlet过滤器

    Spring Security借助一些列Servlet 过滤器 来提供 各种 安全性功能. 我们只需要在应用中的 web.xml 中配置 一个过滤器. <filter> <filte ...

  2. react之引用echarts

    react之引用echarts npm: npm install echarts --save 代码: import React, { Component } from 'react'; // 引入 ...

  3. 跟我学算法-svm支持向量机算法推导

    Svm算法又称为支持向量机,是一种有监督的学习分类算法,目的是为了找到两个支持点,用来使得平面到达这两个支持点的距离最近. 通俗的说:找到一条直线,使得离该线最近的点与该线的距离最远. 我使用手写进行 ...

  4. django多对多数据库建立 以及数据的传输 并进行增加 删除 修改

    多对多数据库的建立 class Host(models.Model): nid = models.AutoField(primary_key=True) #自增id hostname = models ...

  5. nodepad++快捷键

    在用notepad++进行代码编辑的过程中,其实notepad++也可以进行单行.多行.区块注释和取消注释的....... 快捷键如下: 单行.多行注释              //方式       ...

  6. CLR如何控制类型中的字段排序

    [StructLayout(LayoutKind.Sequential) ]保持字段布局 [StructLayout(LayoutKind.Auto) ]CLR自动选择最优 [StructLayout ...

  7. Spring中的AOP(五)——定义切入点和切入点指示符

    定义切入点 在前文(点击查看)中使用到的AdviceTest类中同一个切点(即* com.abc.service.*.advice*(..)匹配的连接点)却重复定义了多次,这显然不符合软件设计的原则, ...

  8. quartz cron表达式demo

    1.每天0点5分开始,以后每隔15分钟启动一次,23:50停止: 0 5/15 0-23 * * * cron表达式,由于从5分开始,每15分钟执行一次,到了23点的最后一次执行时间正好是23:50. ...

  9. Git学习笔记——从一台电脑上传文件到Github上

    目标:从一台电脑上传文件到Github上 前提: 1.这里假定已在Github上创建了仓库,建立了仓库 2.已在这台电脑上安装了Git客户端 实验环境: 1.Windows 10 64位,已安装了Gi ...

  10. golang之流程控制(注意点)

    Go在流程控制方面特点如下: 没有do和while循环,只有一个广义的for语句 switch语句灵活多变,还可以用于类型判断 if语句和switch语句都可以包含一条初始化子语句 break语句和c ...