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

题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓展中国剩余定理。

具体讲解可以看我昨天的博文:http://www.cnblogs.com/KonjakJuruo/p/5176417.html

//poj2891
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; typedef long long LL;
const LL N=;
LL a[N],b[N];
LL tx,ty; LL exgcd(LL aa,LL bb)
{
if(bb==) {tx=,ty=;return aa;}
LL d=exgcd(bb,aa%bb);
LL x=ty,y=tx-(aa/bb)*ty;
tx=x;ty=y;
return d;
} LL lcu(LL aa,LL bb)
{
LL d=exgcd(aa,bb);
return aa*bb/d;
} int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
// printf("%d\n",(-5)%3);
LL n,a1,b1,x;
while(scanf("%I64d",&n)!=EOF)
{
bool bk=;
for(LL i=;i<=n;i++)
scanf("%I64d%I64d",&a[i],&b[i]);
LL A=a[],B=a[],C=b[]-b[];
LL g=exgcd(A,B);
if(C%g) bk=;
else {
x=((tx*C/g)%(B/g)+(B/g))%(B/g);
b1=a[]*x+b[];
a1=lcu(a[],a[]);
}
for(LL i=;i<=n;i++)
{
A=a1,B=a[i],C=b[i]-b1;
g=exgcd(A,B);
if(C%g) {bk=;break;}
x=((tx*C/g)%(B/g)+(B/g))%(B/g);
b1=a1*x+b1;
a1=lcu(a1,a[i]);
}
if(bk) printf("%I64d\n",b1);
else printf("-1\n");
}
return ;
}

【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组的更多相关文章

  1. HDU-3579-Hello Kiki (利用拓展欧几里得求同余方程组)

    设 ans 为满足前 n - 1个同余方程的解,lcm是前n - 1个同余方程模的最小公倍数,求前n个同余方程组的解的过程如下: ①设lcm * x + ans为前n个同余方程组的解,lcm * x ...

  2. 【hdu3579-Hello Kiki】拓展欧几里得-同余方程组

    http://acm.hdu.edu.cn/showproblem.php?pid=3579 题解:同余方程组的裸题.注意输出是最小的正整数,不包括0. #include<cstdio> ...

  3. 【hdu1573-X问题】拓展欧几里得-同余方程组

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 求小于等于N的正整数中有多少个X满足: X mod a0 = b0 X mod a1 = b1 …… X  ...

  4. 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)

    0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...

  5. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  6. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

  7. POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...

  8. poj2891 Strange Way to Express Integers poj1006 Biorhythms 同余方程组

    怎样求同余方程组?如: \[\begin{cases} x \equiv a_1 \pmod {m_1} \\ x \equiv a_2 \pmod {m_2} \\ \cdots \\ x \equ ...

  9. POJ-2891 Strange Way to Express Integers(拓展中国剩余定理)

    放一个写的不错的博客:https://www.cnblogs.com/zwfymqz/p/8425731.html POJ好像不能用__int128. #include <iostream> ...

随机推荐

  1. 3.Knockout.Js(属性绑定)

    前言 让visible绑定到DOM元素上,使得该元素的hidden或visible取决于绑定的值. 简单的绑定 首先还是先定义一个ViewModel var AppViewModel = { shou ...

  2. iOS代码实践总结

    转载地址:http://mobile.51cto.com/hot-492236.htm 最近一个月除了专门抽时间和精力重构之外,还有就是遇到需要添加功能的模块的时候,由于项目中的代码历史因素比较多,第 ...

  3. 防止被dylib hook的小技巧

    在Build Settings中找到“Other Linker Flags”在其中加上”-Wl,-sectcreate,__RESTRICT,__restrict,/dev/null”即可.

  4. PSP0表格

    一 项目计划日志 周活动总结表 姓名:陆宇                                                                         日期:3.1 ...

  5. 新 四则运算题目 C++

    源代码: #include <stdlib.h>#include <iostream.h>#include <conio.h>#include <time.h ...

  6. 6、android 普通日志输出到SD卡

    这是本人见过写博文最负责的一个人: http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_ca ...

  7. IOS常用加密Encryption

    NSString+Encryption.h // // NSString+Encryption.h // haochang // // Created by Administrator on 14-4 ...

  8. [bzoj 3687]简单题 bitset的运用

    题意 给定一个正整数集,求所有子集算术和的异或和   题解 每次加入一个元素x,用原集合a xor (a<< x) 然后每一个值统计一下 bitset看起来很优越,是一个能位运算的布尔数组 ...

  9. 项目分析(channelid是如果产生的)

    channelid如何生成的 /////////////////////////////////////////////////////////////// // // I_TCPServer.h / ...

  10. 如何混合使用ARC和非ARC

    如果你的项目使用的非ARC模式,则为ARC模式的代码文件加入-fobjc-arc标签.如果你的项目使用的ARC模式,则为非ARC模式的代码文件加入 -fno-objc-arc标签.添加标签的方法: 1 ...