[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' ...
随机推荐
- POJ3624 Charm Bracelet 【01背包】
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22621 Accepted: 10157 ...
- poj 2488 A Knight's Journey(dfs+字典序路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...
- cocos2d-x-lua基础系列教程一(hello lua)
myscene.lua function ERROR_TRACBACK(msg) print (==========) print ("lua error is "..tostri ...
- Oracle 学习笔记 13 -- 控制用户权限
数据库控制语言的功能室控制用户对数据库的存取权限. 用户对某类数据具有何种操作权限是有DBA决定的.Oracle 通过GRANT语句完毕权限的授予,通过REVOKE语句完毕对权限的收回. 权限分为系统 ...
- @dynamic与@synthesize的差别
如今非常多时候我们都已经不再使用@synthesizekeyword了,可是须要了解当中的原理: 一.@dynamic与@synthesize的差别 @property有两个相应的词.一个是@synt ...
- nyoj--90--整数划分(母函数)
整数划分 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 将正整数n表示成一系列正整数之和:n=n1+n2+-+nk, 其中n1≥n2≥-≥nk≥1,k≥1. 正整数 ...
- 项目结合activiti工作流框架使用
项目结合activiti工作流框架使用: 1.项目与工作流框架的结合. 2.状态:草稿(待审批).审批中.审批通过.审批失败 3. 提交审批: 0 草稿(待审批),记录绑定工作流执行id,审批状态设置 ...
- js函数 DOM操作
回学校了两天请了两天假,数组和方法的内容周末一定补上! 今天介绍一下JavaScript函数 Function 一.基础内容 1.定义 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. f ...
- (转载) android项目大全,总有你所需的
目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选 程序员8月书讯 项目管理+代码托管+文档协作,开发更流畅 [置顶] android项目大全,总有你所需的 标签: 源 ...
- 【原创】Apache和基于虚拟主机的Tomcat集群方案
最近建设了北京某政府机构的网站,网站前段使用Apache做负载均衡,后端使用Tomcat做的集群,基于虚拟主机的方式访问,并且实现了静态资源和动态资源的分离. 开始的建设方案有两种,一种是使用apac ...