http://acm.hdu.edu.cn/showproblem.php?pid=3579

题解:同余方程组的裸题。注意输出是最小的正整数,不包括0。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; typedef long long LL;
const int N=; LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b==) {x=,y=; return a;}
LL tx,ty;
LL d=exgcd(b,a%b,tx,ty);
x=ty;
y=tx-(a/b)*ty;
return d;
} int main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
int n,T;
scanf("%d",&T);
LL a[N],b[N];
for(int TT=;TT<=T;TT++)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
for(int i=;i<=n;i++) scanf("%I64d",&b[i]);
LL a1=a[],b1=b[];
bool bk=;
for(int i=;i<=n;i++)
{
LL A=a1,B=a[i],C=b[i]-b1,x,y;
LL g=exgcd(A,B,x,y);
if(C%g) {bk=;break;}
x=((x*C/g)%(B/g)+(B/g))%(B/g);
b1=a1*x+b1;
a1=a1/g*a[i];
}
if(!bk) printf("Case %d: -1\n",TT);
else{
/*
a1x+b1=P;(a1>=0,P求的是最小正整数)
b1!=0 --> x=0,b1=P
b1==0 --> x=1,a1=P
*/
if(b1) printf("Case %d: %I64d\n",TT,b1);
else printf("Case %d: %I64d\n",TT,a1);
}
}
return ;
}

【hdu3579-Hello Kiki】拓展欧几里得-同余方程组的更多相关文章

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

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

  2. 【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组

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

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

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

  4. NOIP2012拓展欧几里得

    拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...

  5. poj 1061 青蛙的约会 拓展欧几里得模板

    // poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...

  6. bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得

    这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...

  7. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  8. POJ1061 青蛙的约会-拓展欧几里得

    Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...

  9. BZOJ-2242 计算器 快速幂+拓展欧几里得+BSGS(数论三合一)

    污污污污 2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2312 Solved: 917 [Submit][S ...

随机推荐

  1. file_get_contents函数和curl函数不能用

    某天file_get_contents()突然不能用了,检查了下php配置文件 allow_url_fopen=on 没问题   各种重启也没用 最后在ssh下执行 chmod 755 /etc/re ...

  2. Spark 3000门徒第二课scala面向对象总结

    昨晚听了王家林老师3000门徒spark系列课程的第二课,讲述了scala面向对象知识,并且带着过了一遍Spark核心类:SparkContent,RDD的代码,下面写一下心得: RDD是抽象类,实现 ...

  3. QT中实现中文的显示与国际化

    1 增加头文件    #include "QTextCodec" 2 在文件中增加如下内容 QTextCodec::setCodecForTr(QTextCodec::codecF ...

  4. JavaScript构建(编绎)系统大比拼:Grunt vs. Gulp vs. NPM

    Nicolas Bevacqua进行了一个比较JavaScript构建(编绎)系统的任务.他对三巨头: Grunt, Gulp and NPM进行了比较,并讨论了每种的优缺点. By Nicolas ...

  5. [rsync+inotify]——监控客户端文件变化,rsync同步到服务器

    关于rsync的配置请参考博文:http://www.cnblogs.com/snsdzjlz320/p/5630695.html 实验环境 (1) Rsync服务器:10.0.10.158 (2) ...

  6. sqlserver复杂排序(order by case when)

     /*表 sysid自增主键  scro分数  oper操作时间 scro分数 > 5的按照  分数 降序, 分数小于等于5的按照 操作时间 升序; >5 的排在 <=5的前面*/ ...

  7. Querying mergeinfo requires version 3 of the FSFS filesystem schema

    环境: jdk 1.7; svn  3.0.4;  TortoiseSVN 1.7.13 Subversion 1.7.10; IntelliJ IDEA 13.1.1;win7 64位系统 之前那个 ...

  8. org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

    org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...

  9. background-origin

    background-origin 设置元素背景图片的原始起始位置. 语法: background-origin : border-box | padding-box | content-box; 参 ...

  10. unity 协同

    void Update () { if(Input .GetKeyDown (KeyCode .W )) { StartCoroutine ("Test"); } } IEnume ...