CF - Profact(DFS + 剪枝)

A - Profact
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

standard input/output 
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

Input
7
1
2
3
8
12
24
25
Output
YES
YES
NO
YES
YES
YES
NO

Hint

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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值