HDU 3579 Hello Kiki 中国剩余定理(合并方程

题意:

给定方程

res % 14 = 5

res % 57 = 56

求res

中国剩余定理裸题

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<queue>
#include<vector>
using namespace std;
#define N 10005
#define ll __int64
ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
//求一组解(x,y)使得 ax+by = gcd(a,b), 且|x|+|y|最小(注意求出的 x,y 可能为0或负数)。
//下面代码中d = gcd(a,b)
//可以扩展成求等式 ax+by = c,但c必须是d的倍数才有解,即 (c%gcd(a,b))==0
void extend_gcd (ll a , ll b , ll& d, ll &x , ll &y) {
	if(!b){d = a; x = 1; y = 0;}
	else {extend_gcd(b, a%b, d, y, x); y-=x*(a/b);}
}
ll inv(ll a, ll n) { //计算%n下 a的逆。如果不存在逆return -1;
	ll d, x, y;
	extend_gcd(a, n, d, x, y);
	return d == 1 ? (x+n)%n : -1;
}
ll n[N],b[N],len,lcm;
ll work(){
	for(ll i = 2; i <= len; i++) {
		ll A = n[1], B = n[i], d, k1, k2, c = b[i]-b[1];
		extend_gcd(A,B,d,k1,k2);
		if(c%d)return -1;
		ll mod = n[i]/d;
		ll K = ((k1*(b[i]-b[1])/d)%mod+mod)%mod;
		b[1] = n[1]*K + b[1];
		n[1] = n[1]*n[i]/d;
	}
	if(b[1]==0)return lcm;
	return b[1];
}
int main(){
	ll i,T,Cas=1;cin>>T;
	while(T--){
		cin>>len;
		lcm = 1;
		for(i=1;i<=len;i++) {
			cin>>n[i];
			lcm = lcm / gcd(lcm,n[i]) * n[i];
		}
		for(i=1;i<=len;i++)cin>>b[i];
		cout<<"Case "<<Cas++<<": ";
		cout<<work()<<endl;
	}
	return 0;
}

HDU 3579 Hello Kiki 中国剩余定理(合并方程

时间: 2024-10-25 20:26:22

HDU 3579 Hello Kiki 中国剩余定理(合并方程的相关文章

hdu 3579 Hello Kiki (中国剩余定理)

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1943    Accepted Submission(s): 693 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins s

hdu 3430 Shuffling(置换群+中国剩余定理)

题目链接:hdu 3430 Shuffling 题意: 给出n张牌,标号为1-n,然后给出两个序列,序列1表示序列1,2,3,4--,n洗一次牌后到达的. 序列2表示目标序列,问初始序列按序列1的洗牌方式洗几次能到达序列2的情况,如果不能到达输出-1. 题解: 在初始序列和序列1的变换中找出1能变到那些牌,这些牌构成一个集合,这些集合中的牌必然是能够相互到达的. 然后在序列2中也找出这样一个集合,集合中这些元素的相互顺序是要一样的,这就是判断能否达到. 然后这样可以列出几个线性同余方程组,用中国

hdu X问题 (中国剩余定理不互质)

http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4439    Accepted Submission(s): 1435 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0],

HDU 5238 Calculator(中国剩余定理+线段树)

题意: 有加,乘,次方3种运算,初始值为x,给定运算式. 现在有2种操作: 第一种:告诉你x的值,求答案模29393. 第二种:更改某个位置的运算. 解析: 线段树维护值域的问题,但是那个操作并不能简单的合并,因为值域还是很大的数组开不下,所以我们得另寻他法. 可以发觉29393并不是质数,29393 = 7×13×17×19. 设: t1=ans%7,t2=ans%13,t3=ans%17,t4=ans%19 那么问题的解就是模方程组 ???????????x≡t1(mod)7x≡t2(mod

hdu 5446 Unknown Treasure 中国剩余定理+lucas

题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define l

hdu 3579 Hello Kiki

http://acm.hdu.edu.cn/showproblem.php?pid=3579 注意下最后的答案等于0是不行的,因为要的是正整数 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define inf (0x3f3f3f3f) typedef long

HDU——3579 Hello Kiki

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4206    Accepted Submission(s): 1616 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins

hdu 3579 Hello Kiki 不互质的中国剩余定理

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一

中国剩余定理 hdu 3579

HDU 3579 Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3107    Accepted Submission(s): 1157 Problem Description One day I was shopping in the supermarket. There was a cashier counti