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.
The first line contains two integers a and m (1 ≤ a, m ≤ 105).
Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No".
1 5
No
3 6
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; }