zoukankan      html  css  js  c++  java
  • CodeForces 485A Factory (抽屉原理)

    A. Factory
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce  (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory.

    The board of directors are worried that the production by the given plan may eventually stop (that means that there will be а moment when the current number of details on the factory is divisible by m).

    Given the number of details a on the first day and number m check if the production stops at some moment.

    Input

    The first line contains two integers a and m (1 ≤ a, m ≤ 105).

    Output

    Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No".

    Sample test(s)
    input
    1 5
    output
    No
    input
    3 6
    output
    Yes

    题意: 给两个数a和m,每天进行一次操作,a = (a+a%m)%m, 问a是否有可能等于0

    思路: 因为a和m小于10^5,而且a每次都要取余,所以a每天操作之后的变成的值肯定小于m,即小于10^5,开个vis数组,记录下a曾经取过什么数,每次操作后判断如果a出现过,那就是进入循环了输出No,如果a==0就符合要求输出Yes。
    根据抽屉原理,最多进行m+1天一定会有重复出现的余数,时间复杂度O(m)。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int INF = 1e9;
    const double eps = 1e-6;
    const int N = 100010;
    int cas = 1;
    
    bool vis[N];
    int a,m;
    
    void run()
    {
        memset(vis,0,sizeof(vis));
        if(a%m==0){
            puts("Yes");
            return;
        }
        while(true)
        {
            a = (a+a%m)%m;
            if(a==0){
                puts("Yes");
                return;
            }
            if(vis[a]){
                puts("No");
                return;
            }
            vis[a]=1;
        }
    }
    
    int main()
    {
        #ifdef LOCAL
    //    freopen("case.txt","r",stdin);
        #endif
        while(cin>>a>>m)
            run();
        return 0;
    }
  • 相关阅读:
    此时的我,就像一个炸药包...
    我焦躁,并不只是心里承受,还是因为我上面担着。
    我们应该顶住压力
    git初识后的一些问题
    我近期应该找个机会休整一下了
    权限管理设计的一些感悟
    一个无用的功能
    关于IE8与FireFox中,button内字体总是有一个不垂直居中的问题
    Android之EditText文本框监听事件
    Android流量统计TrafficStats类的使用
  • 原文地址:https://www.cnblogs.com/someblue/p/4078095.html
Copyright © 2011-2022 走看看