codeforces 485a Factory

来源:互联网 发布:mac如何修改登录密码 编辑:程序博客网 时间:2024/06/11 04:46

水水的一题找下规律。FST了,早就想到会这样= =

题意:

给你a,m。每天进行a = a+(a%m)操作。让你判断,如果最终a不能被m整除,输出No,否则输出yes。

思路:

模拟一下,会发现余数会重复。余数重复后就绝对不可能整除。

复杂度O(m)。

后来看了下素姐代码(最下面),居然是直接判断的,但还是看不懂代码的内涵。

code:

import java.util.*;import java.io.*;public class Main {    static class Solve {        Scanner cin = new Scanner(System.in);        long a, m;        final int MAXM = (int)1e5+5;                boolean []vis = new boolean[MAXM];        boolean solve()        {            a = cin.nextInt();            m = cin.nextInt();            Arrays.fill(vis, false);            long i = m + 10;            while(i > 0)            {                i--;                long tmp = a%m;                if(tmp == 0) return true;                if(vis[(int)tmp])                    return false;                vis[(int)tmp] = true;                a += tmp;                //System.out.println("a = "+a);            }            return true;        }    }            public static void main(String []args) {        Solve ss = new Solve();        if(ss.solve())            System.out.println("Yes");        else            System.out.println("No");    }}

素姐code:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int main(){    int a, m;    while(scanf("%d%d", &a, &m) == 2){        if(a % m == 0)            printf("Yes\n");        else if(m%2 != 0)            printf("No\n");        else if(m%2 == 0){            while(m%2 == 0)                m /= 2;            if(a%m == 0)                printf("Yes\n");            else                printf("No\n");        }    }    return 0;}



0 0
原创粉丝点击