zoukankan      html  css  js  c++  java
  • Codeforces Round #276 (Div. 2)A. Factory(数论)

      这道题可以暴力的一直按要求的方法去做,做1000000次还不能整除m就认为永远不能整除m了(m不超过100000,循环1000000次比较安全了已经)。这种方法可以AC。

    下面深入的分析一下到底循环多少次就可以确定结果:首先有这样一个规律:(a+b)%c=(a%c+b%c)%c,那么这样其实这道题每次就是2*a。官方题解说的好:

    Production will stops iff exists integer K ≥ 0 such a·2K is divisible by m. From this fact follows that K maximum will be about O(log2(m)). So if we modeling some, for example, 20 days and production does not stop, then it will never stop and answer is "No". Otherwise answer is "Yes".

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<cctype>
    #include<sstream>
    using namespace std;
    #define pii pair<int,int>
    #define LL long long int
    const int eps=1e-8;
    const int INF=1000000000;
    const int maxn=1000+5;
    
    int a,m;
    int main()
    {
        //freopen("in8.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        scanf("%d%d",&a,&m);int w=0;
        for(int i=0;i<20;i++)
        {
            if(a%m==0){w=1;puts("Yes");break;}
            a%=m;
            a+=a;
        }
        if(w==0)puts("No");
        //fclose(stdin);
        //fclose(stdout);
        return 0;
    }

    或者不这么想也行,就去找周期性的规律也可以,但是注意周期不一定从第一个数开始,其实只要后面的a%m有在前面出现过了,那就不用再算下去了,肯定后面都一样。

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<cctype>
    #include<sstream>
    using namespace std;
    #define pii pair<int,int>
    #define LL long long int
    const int eps=1e-8;
    const int INF=1000000000;
    const int maxn=1000+5;
    int a,m;
    set<int>s;
    int main()
    {
        cin>>a>>m;
        while(a%m!=0){
            if(s.find(a%m)!=s.end()){cout<<"No"<<endl; return 0;}
            s.insert(a%m);
            a += (a%m);
        }
        cout<<"Yes"<<endl;
    
        return 0;
    }
  • 相关阅读:
    gitlab使用
    小程序顶部导航栏标题不居中
    mpvue中使用vant-weapp
    原生js实现轮播图
    vue无法检测数组的变动
    vue报错TypeError: Cannot read property of undefined 或 Uncaught TypeError: this.myMethod is not a function
    vue绑定style使用需要添加浏览器引擎前缀的 CSS 属性
    《孙子算经》之"物不知数"题:中国剩余定理
    Java大数处理类:BigInteger类和BigDecimal类
    【转】操作系统典型调度算法
  • 原文地址:https://www.cnblogs.com/zywscq/p/4079688.html
Copyright © 2011-2022 走看看