Codeforces Round #360 (Div. 1) B. Remainders Game(中国剩余定理)
2016-07-07 17:06
429 查看
题目链接:点击打开链接
思路:
中国剩余定理, 请看维基百科:点击打开链接
该题我们由已知可以写出n个线性同余方程:x % c[i] == a[i] % c[i]。
那么中国剩余定理中明确指出。 我们可以知道 x % lcm(c[i]) 的值。 那么如果lcm可以整除k而不产生余数, 我们也就知道了x % k的值, 它就等于 x % lcm的值。
细节参见代码:
思路:
中国剩余定理, 请看维基百科:点击打开链接
该题我们由已知可以写出n个线性同余方程:x % c[i] == a[i] % c[i]。
那么中国剩余定理中明确指出。 我们可以知道 x % lcm(c[i]) 的值。 那么如果lcm可以整除k而不产生余数, 我们也就知道了x % k的值, 它就等于 x % lcm的值。
细节参见代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include <cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) using namespace std; typedef long long ll; typedef long double ld; const double eps = 1e-6; const double PI = acos(-1); const int mod = 1000000000 + 7; const int INF = 0x3f3f3f3f; // & 0x7FFFFFFF const int seed = 131; const ll INF64 = ll(1e18); const int maxn = 100; int T,n,k,v; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int main() { scanf("%d%d", &n, &k); scanf("%d", &v); ll ans = v; for(int i = 2; i <= n; i++) { scanf("%d", &v); ans = lcm(ans, v) % k; } if(ans % k == 0) printf("Yes\n"); else printf("No\n"); return 0; }
相关文章推荐
- [Saltstack]-{grains,pillar}[二]
- leetcode336. Palindrome Pairs
- Chainer's simple implementation of fast R-CNN
- http://blog.csdn.net/sd0902/article/details/8395677
- wait、sleep、notify、notifyAll的区别
- ORA-04098: trigger 'SYS.OLAPISHUTDOWNTRIGGER' is invalid and failed re-validation
- VC内存溢出一例 –- 调用约定不一致 (_CRT_DEBUGGER_HOOK(_CRT_DEBUGGER_GSFAILURE)
- adcmctl.sh wait=Y
- sun solairs安装11gr2RAC,界面打不开
- 约束Constraints
- 【USACO TRAINING】数字金字塔
- Confidentiality, Integrity, Availability: The three components of the CIA Triad
- 嵌入式系统开发—RainXXX系统理解1
- Find K Pairs with Smallest Sums
- 出现Assertion failure in -[***** layoutSublayersOfLayer:]
- servermain启动过程
- SRVCTL 中failover_method 解释
- [Array]Contains Duplicate II
- [Array]Contains Duplicate II
- AIX运维常用命令