HDU3579 Hello Kiki(CRT非互质)
2016-07-30 11:46
218 查看
题目点我点我点我
题目大意:跟POJ2891题意基本一致。
解题思路:跟POJ2891解题思路基本一致,详情点击这里
但是需要注意的是这里答案不能为0,因为是求硬币数,因为这个WA了几次orz……
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
//#pragma comment(linker, "/STACK:102400000,102400000")
LL aa[10],mm[10];
LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x = 1, y = 0;
return a;
}
LL p = extend_gcd(b,a%b,x,y);
LL temp = x;
x = y;
y = temp - (a / b) * y;
return p;
}
LL CRT(int n)
{
LL a1,a2,m1,m2,x,y,d,t;
a1 = aa[1],m1 = mm[1];
for(int i=2;i<=n;i++)
{
a2 = aa[i],m2 = mm[i];
d = extend_gcd(a1,a2,x,y);
if((m2-m1)%d)return -1;
t = a2 / d;
x = (x * (m2-m1)/d % t + t) % t;
m1 = a1 * x + m1;
a1 = a1/d*a2;
m1 = (m1 % a1 +a1)%a1;
}
if(m1==0)m1 = a1;
return m1;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int t,n,cas = 1;
t = read();
while(t--)
{
n = read();
for(int i=1;i<=n;i++)scanf("%I64d",&aa[i]);
for(int i=1;i<=n;i++)scanf("%I64d",&mm[i]);
printf("Case %d: %I64d\n",cas++,CRT(n));
}
return 0;
}
题目大意:跟POJ2891题意基本一致。
解题思路:跟POJ2891解题思路基本一致,详情点击这里
但是需要注意的是这里答案不能为0,因为是求硬币数,因为这个WA了几次orz……
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
//#pragma comment(linker, "/STACK:102400000,102400000")
LL aa[10],mm[10];
LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x = 1, y = 0;
return a;
}
LL p = extend_gcd(b,a%b,x,y);
LL temp = x;
x = y;
y = temp - (a / b) * y;
return p;
}
LL CRT(int n)
{
LL a1,a2,m1,m2,x,y,d,t;
a1 = aa[1],m1 = mm[1];
for(int i=2;i<=n;i++)
{
a2 = aa[i],m2 = mm[i];
d = extend_gcd(a1,a2,x,y);
if((m2-m1)%d)return -1;
t = a2 / d;
x = (x * (m2-m1)/d % t + t) % t;
m1 = a1 * x + m1;
a1 = a1/d*a2;
m1 = (m1 % a1 +a1)%a1;
}
if(m1==0)m1 = a1;
return m1;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int t,n,cas = 1;
t = read();
while(t--)
{
n = read();
for(int i=1;i<=n;i++)scanf("%I64d",&aa[i]);
for(int i=1;i<=n;i++)scanf("%I64d",&mm[i]);
printf("Case %d: %I64d\n",cas++,CRT(n));
}
return 0;
}
相关文章推荐
- Spring MVC 源码
- Retrofit源码分析
- 【python自动化】文件名序列化
- 238. Product of Array Except Self [medium] (Python)
- 以UEFI模式安装Ghost Win8方法
- Java网络通信之HttpClient
- Java基础——处理对象
- iOS 代码规范(参考大神博客+自己想法)
- docker
- Servlet防止跨站引用
- 对抗式网络(Adversarial Network)-深度学习突破的机会
- RMB 转大写的一个小demo
- Oracle中经典分页代码!
- Table of Contents - HttpClient
- ios 横竖屏切换总结
- mysql下表的修复与优化
- Ubuntu系统安装搜狗输入法
- juery datatable属性描述以及用法
- laravel学习教程之存取器
- 菜鸟学hadoop(01)