Codeforces 678B Remainders Game(数论)

题意:

给你一个k,再给你一个数组c,让你判断对于任意的非负整数x,知道了 xmodcifori:=1ton 的值,问你是否能够知道 xmodk 的值。
作为一个数学渣,完全没有想法。
其实我们可以从反面来考虑这个问题。假设有两个x1和x2是的上述的条件不成立,依旧是说对于每一个数字 ci x1modci=x2modcix1modkx2modk 。那么可以得到这样一个结论 x1x20(modci)(x1x2)%k0 。这个结论仔细研究发现之后发现很有用,由结论的第一部分我们可以得到一个等式 lcm(c1,c2,c3,...cn)x1x2 ,第二个部分可以得到 lcm(c1,c2,c3,...cn)%k0 。这个有什么用呢?
我们可以很快的构造出 x1=lcm(c1,c2,c3,...cn),x2=lcm(c1,c2,c3,...cn)2 ,继而问题就转变成求 lcm(c1,c2,c3,...cn)%k 是否等于0。因为数字很大,直接求lcm是会溢出的,所以我们要用一个很奇妙的方法,就是gcd和lcm的关系。在CF上有一个人总结的很好:
LCM = union of prime factors.
GCD = intersection of prime factors.
这样的话判断 lcm(c1,c2,c3,...cn)%k=0 就变成了求c数组所有书的质因数并集和k的质因数的交集是否等于k的质因数集合。这样就可以很方便的进行判断,代码实现才考了一个大神的很机智的方法。

代码:

//
//  Created by  CQU_CST_WuErli
//  Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.
//
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <sstream>
#define CLR(x) memset(x,0,sizeof(x))
#define OFF(x) memset(x,-1,sizeof(x))
#define MEM(x,a) memset((x),(a),sizeof(x))
#define BUG cout << "I am here" << endl
#define lookln(x) cout << #x << "=" << x << endl
#define SI(a) scanf("%d", &a)
#define SII(a,b) scanf("%d%d", &a, &b)
#define SIII(a,b,c) scanf("%d%d%d", &a, &b, &c)
const int INF_INT=0x3f3f3f3f;
const long long INF_LL=0x7f7f7f7f;
const int MOD=1e9+7;
const double eps=1e-10;
const double pi=acos(-1);
typedef long long  ll;
using namespace std;

ll n, k;
ll c[1000100];

ll gcd(ll a, ll v) {
    return __gcd(a, v);
}

ll lcm(ll a, ll b) {
    return a * b / gcd(a, b);
}

int main(int argc, char const *argv[]) {
#ifdef LOCAL
    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);
    // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);
#endif
    while(cin >> n >> k) {
        for (int i = 1; i <= n; i++)
            scanf("%I64d", c + i);
       ll tmp = 1;
        for (int i = 1; i <= n; i++) {
            tmp = gcd(k, lcm(tmp, c[i]));
        }
        puts(tmp == k ? "Yes" : "No");
    }
    return 0;
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \|     |//  `.
            /  \|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑        永无BUG
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值