【数论】【中国剩余定理】【LCM】hdu1788 Chinese remainder theorem again

根据题目容易得到N%Mi=Mi-a。

那么可得N%Mi+a=Mi。

两侧同时对Mi取余,可得(N+a)%Mi=0。

将N+a看成一个变量,就可以把原问题转化成求Mi的LCM,最后减去a即可。

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
int K;
ll a;
int main(){
	ll x;
	while(1){
		cin>>K>>a;
		if(K==0 && a==0){
			break;
		}
		ll lcm=1;
		for(int i=1;i<=K;++i){
			scanf("%I64d",&x);
			lcm=lcm/__gcd(lcm,x)*x;
		}
		printf("%I64d\n",lcm-a);
	}
	return 0;
}
时间: 2024-08-10 14:04:35

【数论】【中国剩余定理】【LCM】hdu1788 Chinese remainder theorem again的相关文章

HDU1788 Chinese remainder theorem again【中国剩余定理】

题目链接: pid=1788">http://acm.hdu.edu.cn/showproblem.php?pid=1788 题目大意: 题眼下边的描写叙述是多余的... 一个正整N除以M1余M1-a,除以M2余M2-a.除以M3余M3-a. 即除以Mi余Mi-a(a < Mi < 100),求满足条件的最小的数. 思路: 这是一道中国剩余定理的基础题.由题目得出N % Mi + a = Mi,即得:N + a = 0(mod Mi).也 就是全部的Mi都能整除N+a. 那么题

Chinese remainder theorem again(中国剩余定理)

C - Chinese remainder theorem again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,…,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2(mod m2) … x≡ak(mod mk) 在0<=<m1m2…mk内有唯一解

Chinese remainder theorem again(中国剩余定理+不互质版+hud1788)

Chinese remainder theorem again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1788 Appoint description:  System Crawler  (2015-04-27) Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,m

hdu 1788 Chinese remainder theorem again 最小公倍数

Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2014    Accepted Submission(s): 777 Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,mk两两互素,则下面同余

HDU 1788 Chinese remainder theorem again 中国剩余定理

题意: 给定n,AA 下面n个数m1,m2···mn 则有n条方程 res % m1 = m1-AA res % m2 = m2-AA 问res的最小值 直接上剩余定理,嘿嘿 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #i

HDU——1788 Chinese remainder theorem again

Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3174    Accepted Submission(s): 1371 Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的:假设m1,m2,…,mk两两互素,则下面同余

HDU 1788 Chinese remainder theorem again

题目链接 题意 : 中文题不详述. 思路 : 由N%Mi=(Mi-a)可得(N+a)%Mi=0;要取最小的N即找Mi的最小公倍数即可. 1 //1788 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <iostream> 6 #define LL long long 7 8 using namespace std ; 9 10 LL gcd(LL x,LL y)

初识中国余数定理 (Chinese Remainder Theorem)

初识中国余数定理 (Chinese Remainder Theorem) 中国余数定理介绍 起源: 在<孙子算经>中有这样一个问题: "今有物不知其数,三三数之剩二(除以3余2),五五数之剩三(除以5余3),七七数之剩二(除以7余2),问物几何?" 这个问题称为"孙子问题",该问题的一般解法国际上称为"中国剩余定理". 解法: 设未知数为X 1. 找到三个数: 1). 从3和5的公倍数中找到被7除余1的数,15: 2). 从5和7的公

POJ 1006 Biorhythms (数论-中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111285   Accepted: 34638 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,