题干:
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".
Examples
Input
1 5
Output
No
Input
3 6
Output
Yes
题目大意:
给两个数a和m,每天进行一次操作,a = (a+a%m)%m, 问a是否有可能等于0思路:
解题报告:
本来想直接看gcd、、、但是仔细读题发现不行,因为他这个没大有规律啊、、、所以没有类似的结论可以用,
因为a和m小于10^5,而且a每次都要取余,所以a每天操作之后的变成的值肯定小于m,即小于10^5,开个vis数组,记录下a曾经取过什么数,每次操作后判断如果a出现过,那就是进入循环了输出No,如果a==0就符合要求输出Yes。
根据抽屉原理,最多进行m+1天一定会有重复出现的余数,时间复杂度O(m)。
当然这题还有个O(logm) 的做法:
我们推算两步:(a + (a mod m)) mod m = ((a mod m) + (a mod m)) mod m = (2*a) mod m。也就是说接下来的所有答案都是2的幂次,也就是说 如果存在K ≥ 0 ,使得,那么输出Yes,否则输出No。题目范围1e5,也就是说我们只需要推算大概20步,就可以break了。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n,x,y,a[MAX],ans;
const ll INF = 0x3f3f3f3f3f;
int main() {ll a,m;int flag = 0;scanf("%lld%lld",&a,&m);for(int i = 1; i<=100000; i++) {if(a%m == 0) flag = 1;a = (a+a)%m;}if(flag) puts("Yes");else puts("No");return 0;
}