Description
Announcement
- Statements
Alice is bored out of her mind by her math classes. She craves for something much more exciting. That is why she invented a new type of numbers, the profacts. Alice calls a positive integer number a profact if it can be expressed as a product of one or several factorials.
Just today Alice received n bills. She wonders whether the costs on the bills are profact numbers. But the numbers are too large, help Alice check this!
Input
The first line contains a single integer n, the number of bills (1 ≤ n ≤ 105). Each of the next n lines contains a single integer ai, the cost on the i-th bill (1 ≤ ai ≤ 1018).
Output
Output n lines, on the i-th line output the answer for the number ai. If the number ai is a profact, output "YES", otherwise output "NO".
Sample Input
7 1 2 3 8 12 24 25
YES YES NO YES YES YES NO
Hint
A factorial is any number that can be expressed as 1·2·3·...·k, for some positive integer k, and is denoted by k!.
这道题目只要简单的DFS和剪枝即可
其中剪枝主要是对于素数,众所周知,素数是无法被其他数整除的,那么如果一个数分为几个阶乘的乘积,这些阶乘里面有
素数的存在,那么一定要有素数!构成,那么如果一个素数的阶乘无法构成那么就没有必要继续递归下去了。
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("D://imput.txt", "r", stdin)
typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
int d = p < q ? 1 : -1;
while(p != q) {
cout << *p;
p += d;
if(p != q) cout << Gap;
}
cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
int len = bes.length();
if(len >= 2)cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}
const int INF = 0x3f3f3f3f;
const int MAXM = 2e5;
const int MAXN = 20 + 5;
const int mod = 1e9 + 7;
int n;
LL a;
LL X[MAXN];
int A[MAXN] = {2,3,5,7,11,13,17,19};
bool success;
void init() {
X[0] = 1;
for(int i = 1; i <= 20; i ++) X[i] = X[i - 1] * i;
}
void dfs(int id, LL x) {
if(x <= 1) {
success = true;
return;
}
if(success || id <= 1) return;
if(x % X[id] == 0) dfs(id, x / X[id]);
if(!success) {
for(int j = 0; j <= 7; j ++) {
if(A[j] == id && x % A[j] == 0) return;
if(A[j] > id) break;
}
dfs(id - 1, x);
}
}
int main() {
init();
while(~scanf("%d", &n)) {
for(int i = 0; i < n; i ++) {
scanf("%I64d", &a);
success = false;
dfs(20, a);
printf(success ? "YES\n":"NO\n");
}
}
return 0;
}