LightOJ 1189 - Sum of Factorials(贪心)
来源:互联网 发布:网络水军有多少 编辑:程序博客网 时间:2024/04/29 16:45
Description
Given an integer n, you have to find whether it can be expressed as summation of factorials. For givenn, you have to report a solution such that
n = x1! + x2! + ... + xn! (xi < xj for all i < j)
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 1018).
Output
For each case, print the case number and the solution in summation of factorial form. If there is no solution then print'impossible'. There can be multiple solutions, any valid one will do. See the samples for exact formatting.
Sample Input
4
7
7
9
11
Sample Output
Case 1: 1!+3!
Case 2: 0!+3!
Case 3: 1!+2!+3!
Case 4: impossible
比赛的时候以为 10的18次方会爆long long, 敲了个高精度模板,就不知道要怎么写了。傻啊>.<
long long : 10的18次方,int 10的4次方。
打阶乘表,以每个数为基点,从后往前加。
CODE:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long ll;ll num[20];int ans[20];void cal(){ num[0] = 1; num[1] = 1; for(int i = 2; i < 21; ++i) { num[i] = num[i - 1] * i; }}int main(){//freopen("in", "r", stdin); int T; scanf("%d", &T); cal(); printf("%lld\n", num[20]); int cas = 0; while(T--) { cas++; printf("Case %d: ", cas); ll n, s; scanf("%lld", &n); int as; for(int i = 0; i < 21; ++i) { if(num[i] > n) { printf("impossible\n"); break; } //使用这样的判断,要计算到20,否则当n大于19!但是小于20!时则没有答案输出。 as = 0; s = 0; ll nn = n; for(int j = i; j >= 0; --j) { if(nn - num[j] >= 0) { nn -= num[j]; ans[as++] =j; if(nn == 0) { printf("%d!", ans[as-1]); for(int k = as - 2; k >= 0; --k) { printf("+%d!", ans[k]); } printf("\n"); break; } } } if(nn == 0) break; } } return 0;}
- LightOJ 1189 - Sum of Factorials(贪心)
- LightOJ 1189 - Sum of Factorials(贪心+阶乘)
- LightOJ 1189 - Sum of Factorials
- 【LightOJ】1189 - Sum of Factorials(思维)
- lightoj-1189-Sum of Factorials【思维】
- POJ 1775 FZU 1106 LightOJ 1189 Sum of Factorials
- LIGHT OJ 1189 - Sum of Factorials【贪心+递归】
- light oj 1189 - Sum of Factorials (阶乘+贪心)
- POJ 1775 Sum of Factorials DFS 贪心
- 贪心法_题目1038:Sum of Factorials
- light oj 1189 - Sum of Factorials (数学)
- Sum of Factorials
- Sum of Factorials
- Sum of Factorials
- Sum of Factorials zoj
- poj1775 - Sum of Factorials
- 1038.Sum of Factorials
- fjnu 1050 Sum of Factorials
- 安装eclipse maven插件(手动离线安装)
- html5学习4:HTML5之input:file说明
- 机器学习与智能优化 之 学习有道
- POJ 2739 - Sum of Consecutive Prime Numbers(素数筛法+前缀和 / 尺取法)
- mahout-0.9的安装配置
- LightOJ 1189 - Sum of Factorials(贪心)
- 用绘制控件绘制要素示例(openlayers)
- [System Design]Scalability for Dummies学习笔记
- LightOJ 1078 - Integer Divisibility(取模运算)
- 从网络获取时实天气信息并动态生成
- 树莓派-上手体验1
- X-UA-Compatible 兼容模式
- Memory access ordering Part 1 - an introduction
- Memory access ordering part 2 - barriers and the Linux kernel