UVa10856 - Recover Factorial(预处理+二分)
来源:互联网 发布:千寻网络位置服务 编辑:程序博客网 时间:2024/06/11 15:57
Factorial numbersare expressible as the multiplication of zero or more prime numbers. Forexample 4! (Factorial of 4) can be expressed as follows:-
4! = 2 x 2 x 2 x 3(total number of prime factor is 4)
Given N, thenumber of prime factors in X! (Factorial of X),you have to findthe minimum possible value ofX.
Input
There may be at most1000 test cases. Each test case consists of one non-negative integerN<=10000001in each line. A negative integer marks the end of input, which should not be processed by your program.
Output
For every test caseexcept last one print either “Case #: X!” if solution exist or“Case#: Not possible.” if no solution exist in each line (without the quotes).Here ‘#’ represents serial of test case starting from 1. Look at sample outputfor details.
Sample Input
4
240
241
-1
Sample Output
Case 1: 4!
Case 2: 101!
Case 3: Not possible.
先预处理,然后用二分法查找
#include <cstdio>#include <cstring>using namespace std;const int N = 2705000;const int SQRTN = 1645;unsigned int f[N + 1];void init();int solve(int n);int main(){#ifndef ONLINE_JUDGEfreopen("d:\\OJ\\uva_in.txt", "r", stdin);#endifinit();int t = 1, n;while (scanf("%d", &n) == 1 && n >= 0) {int ans = solve(n);if (ans == -1) {printf("Case %d: Not possible.\n", t++);} else printf("Case %d: %d!\n", t++, solve(n));}return 0;}void init(){unsigned int i, j, k;memset(f, 0x00, sizeof(f));for (i = 2; i <= N; i++) {if (f[i]) continue;for (j = i; j <= N; j += i) {f[j]++;}if (i > SQRTN) continue;for (k = i * i; ; k *= i) {for (j = k; j <= N; j += k) f[j]++;if ((long long)k * (long long)i > (long long)N) break;}}for (i = 1; i <= N; i++) {f[i] += f[i - 1];}}int solve(int n){int a, b, c;if (n == 0) return 0;for (a = 0, b = N; a <= b;) {c = (a + b) >> 1;if (f[c] < n) {a = c + 1;} else if (f[c] > n) {b = c - 1;} else return c;}return -1;}
- UVa10856 - Recover Factorial(预处理+二分)
- UVA - 10856 Recover Factorial(二分查找)
- SSU 154 Factorial(二分)
- 154. Factorial (二分+数论)
- HRBUST 1365 预处理+二分+?
- SPOJ - PHT【二分+预处理】
- SGU 154 Factorial(数论+二分)
- poj 1003 [离线预处理 + 二分]
- hdu 4791 dp预处理+二分
- Factorial!
- Factorial
- Factorial
- factorial
- Factorial
- Factorial
- Factorial
- Factorial
- Factorial
- C++强大背后
- 进程间的通信方式汇总
- poj 2096 Collecting Bugs 概率dp(期望)
- Jasper Report Percentage/Currency/Number Format
- C语言全局变量的初始化
- UVa10856 - Recover Factorial(预处理+二分)
- oracle 导出页面grid数据的存储过程
- Kses PHP HTML 过滤类
- 邻接矩阵实现图+深度/广度优先遍历+最小生成树
- win32 SDK 响应按扭消息
- 纯linux测试程序
- IOS后台运行机制 与 动作
- Linux用户组的相关命令
- Java集合框架的层次