zoukankan      html  css  js  c++  java
  • CodeForces-668D:Remainders Game (中国剩余定理||理解)

    Today Pari and Arya are playing a game called Remainders.

    Pari chooses two positive integer x and k, and tells Arya k but not x. Arya have to find the value . There are n ancient numbers c1, c2, ..., cn and Pari has to tell Arya  if Arya wants. Given k and the ancient values, tell us if Arya has a winning strategy independent of value of x or not. Formally, is it true that Arya can understand the value  for any positive integer x?

    Note, that  means the remainder of x after dividing it by y.

    Input

    The first line of the input contains two integers n and k (1 ≤ n,  k ≤ 1 000 000) — the number of ancient integers and value k that is chosen by Pari.

    The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 1 000 000).

    Output

    Print "Yes" (without quotes) if Arya has a winning strategy independent of value of x, or "No" (without quotes) otherwise.

    Example

    Input
    4 5
    2 3 5 12
    Output
    Yes
    Input
    2 7
    2 3
    Output
    No

    题意:给定N,K。输入N个ni,表示已知X%ni的值,有了N组这样的剩余系,问是否X%K的值唯一。

    思路:求出Lcm(ni),若Lcm(ni)%K==0,则唯一。

    原因:因为中国剩余定理:ans=Σ(Ai*Ni*Mi)%N,而N就是Lcm(ni)。如果N是K的倍数,那么先%N,再%K的结果是不变的。

     

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define ll long long
    ll gcd(ll a,ll b)
    {
        if(b==0) return a;
        return gcd(b,a%b);
    }
    int main()
    {
        ll N,K,x,res=1;
        scanf("%lld%lld",&N,&K);
        for(int i=1;i<=N;i++){
            scanf("%lld",&x);
            res=x/gcd(x,res)*res%K;
        }
        if(res%K==0) printf("Yes
    ");
        else printf("No
    ");
        return 0;
    }
  • 相关阅读:
    20201226 ZooKeeper
    20201220 分布式理论、架构设计(自定义RPC)
    20201217 Cluster模式潜在问题及解决方案、Web服务综合解决方案
    20201216 Nginx
    20201214 Tomcat
    20201209 Spring Boot
    20201206 Spring Data JPA
    20201205 Spring MVC
    20201128 IoC容器设计实现及Spring源码分析
    numpy数组及处理:效率对比
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8559660.html
Copyright © 2011-2022 走看看