LightOJ 1189 - Sum of Factorials(贪心+阶乘)
来源:互联网 发布:淘宝评论怎么删除手机 编辑:程序博客网 时间:2024/05/16 05:10
Description
Given an integer n, you have to find whether it can be expressed as summation of factorials. For given n, 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
这一题题意就是给你一个数,问哪些数的阶乘等于这个数,并按照样例从小到大输出这些数的每一项,其中每一项只可以用一次,如果不存在这些数,就输出impossible。
贪心的题型,每次考虑最大的放入里面,从后往前找。
#include<cstdio>long long p[21]={1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,121645100408832000,2432902008176640000};int main(){int t,cas=1;scanf("%d",&t);while(t--){int a[20],flag=0; long long n;scanf("%lld",&n); for(int i=20;i>=0;i--) {if(n>=p[i]){n=n-p[i];a[flag++]=i;} }printf("Case %d: ",cas++);if(n==0){for(int i=flag-1;i>=0;i--){if(i==flag-1) printf("%d!",a[i]);else printf("+%d!",a[i]);}printf("\n");}else printf("impossible\n"); } return 0;}
- LightOJ 1189 - Sum of Factorials(贪心+阶乘)
- LightOJ 1189 - Sum of Factorials(贪心)
- LightOJ 1189 - Sum of Factorials
- light oj 1189 - Sum of Factorials (阶乘+贪心)
- 【LightOJ】1189 - Sum of Factorials(思维)
- lightoj-1189-Sum of Factorials【思维】
- POJ 1775 FZU 1106 LightOJ 1189 Sum of Factorials
- zoj 2358||poj1775 Sum of Factorials(阶乘)
- LIGHT OJ 1189 - Sum of Factorials【贪心+递归】
- Lightoj1189——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
- daterangepicker的时间参数问题
- bp神经网络
- CRF 简介入门 .
- 聊聊Google官方MVP
- opengles绘制圆柱体(光照+纹理)
- LightOJ 1189 - Sum of Factorials(贪心+阶乘)
- js 插入排序
- iOS开发:通讯录获取
- Java 访问权限
- 出现的问题:error: No resource identifier found for attribute 'layou_width' in package 'android'
- javascript正则表达式
- android串口通信以及串口协议解析
- javascript犀牛书学习笔记(一)Web浏览器中的Javascript
- 一个网站完整详细的SEO优化方案