D. Product of Binary Decimals
time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Let’s call a number a binary decimal if it is a positive integer and all digits in its decimal notation are either 0 0 0 or 1 1 1. For example, 1 010 111 1\,010\,111 1010111 is a binary decimal, while 10 201 10\,201 10201 and 787 788 787\,788 787788 are not.
Given a number n n n, you are asked whether or not it is possible to represent n n n as a product of some (not necessarily distinct) binary decimals.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 5 ⋅ 1 0 4 1 \leq t \leq 5 \cdot 10^4 1≤t≤5⋅104) — the number of test cases.
The only line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1≤n≤105).
Output
For each test case, output “YES” (without quotes) if n n n can be represented as a product of binary decimals, and “NO” (without quotes) otherwise.
You can output “YES” and “NO” in any case (for example, strings “yES”, “yes”, and “Yes” will be recognized as a positive response).
Example
inputCopy
11
121
1
14641
12221
10110
100000
99
112
2024
12421
1001
outputCopy
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
YES
Note
The first five test cases can be represented as a product of binary decimals as follows:
- 121 = 11 × 11 121 = 11 \times 11 121=11×11.
- 1 = 1 1 = 1 1=1 is already a binary decimal.
- 14 641 = 11 × 11 × 11 × 11 14\,641 = 11 \times 11 \times 11 \times 11 14641=11×11×11×11.
- 12 221 = 11 × 11 × 101 12\,221 = 11 \times 11 \times 101 12221=11×11×101.
- 10 110 = 10 110 10\,110 = 10\,110 10110=10110 is already a binary decimal.
AC代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
//#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=998244353;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e8 + 10;
int t;
bool check(int x)
{
while(x){
int l = x % 10;
x /= 10;
if(l != 0 && l != 1) return false;
}
return true;
}
//如果感到被绕进去了就重新规划一下自己的思路
int main()
{
cin >> t;
while(t --){
int n;
cin >> n;
if(check(n)) puts("YES");
else {
int flag = 0;
for(int i = 2; i <= n / i; i ++)
{
if(n % i == 0)
{
int x = n, k = n, y = i, s = 0;
while(x % i == 0 && x != 0){
x /= y;
s ++;
}
int p = k / pow(y, s);
if(check(p) && check(y)) flag = 1;
}
}
if(flag) puts("YES");
else puts("NO");
}
}
return 0;
}