[poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891
题目大意:
求解同余方程组,不保证模数互质
题解:
扩展中国剩余定理板子题
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll; const int N=+;
int k;
ll m[N],a[N];
inline ll read()
{
char ch=getchar();
ll s=,f=;
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<='') {s=(s<<)+(s<<)+ch-'';ch=getchar();}
return s*f;
}
ll mul(ll a,ll b,ll mod)
{
ll re=;
for (;b;b>>=,a=(a+a)%mod) if (b&) re=(re+a)%mod;
return re;
}
ll exgcd(ll a,ll b,ll &x0,ll &y0)
{
if (!b)
{
x0=;y0=;
return a;
}
ll gcd=exgcd(b,a%b,y0,x0);
y0-=a/b*x0;
return gcd;
}
ll excrt()
{
ll M=m[],x=a[];
for (int i=;i<=k;i++)
{
ll mi=m[i],ai=a[i];
ll x0,y0;
ll gcd=exgcd(M,mi,x0,y0);
ll c=(ai-x%mi+mi)%mi;
if (c%gcd) return -;
x0=mul(x0,c/gcd,mi/gcd);
//x0=x0*c/gcd%(mi/gcd);
x+=x0*M;
M=M/gcd*mi;
x%=M;
}
return (x%M+M)%M;
}
int main()
{
//freopen("excrt.in","r",stdin);
//freopen("excrt.out","w",stdout);
//scanf("%d",&k);
while (~scanf("%d",&k))
{
for (int i=;i<=k;i++) m[i]=read(),a[i]=read();
printf("%lld\n",excrt());
}
return ;
}
[poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)的更多相关文章
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- 「POJ2891」Strange Way to Express Integers【数学归纳法,扩展中国剩余定理】
题目链接 [VJ传送门] 题目描述 给你\(a_1...a_n\)和\(m_1...m_n\),求一个最小的正整数\(x\),满足\(\forall i\in[1,n] \equiv a_i(mod ...
- 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 ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- 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 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】
求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...
随机推荐
- Cocos2d-x-lua学习点滴
Lua下的方法.自己项目经验,个人见解,不能确保正确. Sprite: local Light = CCSprite:create("light.png") ...
- 百度地图----->地图类型、定位模式、实时交通、我的位置、加入覆盖物、覆盖物详情及提示
在百度地图开发平台 http://developer.baidu.com/map/index.php? title=androidsdk 进行创建应用,获取应用的AK,在进行下载BaiduLBS_An ...
- 【Java】【Flume】Flume-NG启动过程源代码分析(一)
从bin/flume 这个shell脚本能够看到Flume的起始于org.apache.flume.node.Application类,这是flume的main函数所在. main方法首先会先解析sh ...
- UVA 11077 Find the Permutations 递推置换
Find the Permutations Sorting is one of the most used operations in real ...
- 1.future线程通信
#include <future> #include<iostream> #include <thread> #include <thread> #in ...
- POJ 1948 DP
题意:给你n个木棍(n<=40)每个木棍长度<=40,问用上所有的木棍拼成的三角形的面积的最大值,并输出面积*100的值(不四舍五入) 如果没有解,输出-1. 思路: 背包判断可达性. f ...
- (转载)Android之三种网络请求解析数据(最佳案例)
[置顶] Android之三种网络请求解析数据(最佳案例) 2016-07-25 18:02 4725人阅读 评论(0) 收藏 举报 分类: Gson.Gson解析(1) 版权声明:本文为博主原创 ...
- Servlet学习(五)——通过response设置响应体及中文乱码问题
1.响应体设置文本 PrintWriter writer=response.getWriter(); 获得字符流,通过字符流的write(String s)方法可以将字符串设置到response 缓冲 ...
- QNX多线程同步之Barrier(屏障)
之前和大家介绍过QNX上的线程同步方法metux和semophore,通过这两种方法可以对一个或者几个资源进行加锁,避免资源使用上的冲突.在另一种情况下,某个线程需要在其它线程完成工作后才继续执行,这 ...
- rsyslog 存储到 mysql
输出Host1/2的系统日志, 记录到mysql服务器数据库中, 并发布loganalyzer 结构关系如下图: 思路: 通过远程连接mysql, 使得rsyslog的log记录能够写入到mysql中 ...