CF - Profact(DFS + 剪枝)
来源:互联网 发布:js 数组和对象 编辑:程序博客网 时间:2024/05/29 01:52
Description
Announcement
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
71238122425
YESYESNOYESYESYESNO
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;}
- CF - Profact(DFS + 剪枝)
- Gym 100796K Profact(爆搜+剪枝)
- DFS(剪枝)_hdu_1010
- Friends(DFS+剪枝)
- hdu5113(dfs+剪枝)
- hdu1010(dfs+剪枝)
- dfs+剪枝(hdu1010)
- 等边三角形(dfs剪枝)
- HDU5937(DFS+剪枝)
- Cubes(DFS+剪枝)
- hdu1010(dfs+剪枝)
- hdu 5012 dfs+剪枝
- POJ2248Addition Chains(dfs+剪枝)
- hdu 1010 (DFS+剪枝)
- poj1190生日蛋糕(dfs+剪枝)
- HDOJ(1010)DFS+剪枝
- Zipper(poj2192)dfs+剪枝
- Sticks(DFS+剪枝+贪心)
- 百度ueditor struts2图片上传问题解决方案
- Linux命令date日期时间和Unix时间戳互转
- HTML5入门(1)
- git使用技巧
- App store审核标准(2015.3)中文版
- CF - Profact(DFS + 剪枝)
- Android Resource Types之String简介
- Android的过滤机制
- 字符串和日期的相互转换
- C语言基础第一篇--Hello world !
- 数据结构 单链表 头插 尾插 指定位置前插后插 以及 删除指定元素 C++ 面向对象方法实现
- c和c++中的一维数组和二维数组的动态分配内存,以及参数传递
- [转载]iOS 获取WIFI,IP地址
- macOSX下,如何永久更改Mysql的字符编码格式