链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1781
Description
输入两个正整数 n, m,输出 n!/m!,其中阶乘定义为 n!= 1*2*3*...*n (n>=1)。 比如,若 n=6, m=3,则 n!/m!=6!/3!=720/6=120。
是不是很简单?现在让我们把问题反过来:输入 k=n!/m!,找到这样的整数二元组(n,m) (n>m>=1)。
如果答案不唯一,n 应该尽量小。比如,若 k=120,输出应该是 n=5, m=1,而不是 n=6, m=3,因为 5!/1!=6!/3!=120,而 5<6。
Input
输入包含不超过 100 组数据。每组数据包含一个整数 k (1<=k<=10^9)。
Output
对于每组数据,输出两个正整数 n 和 m。无解输出"Impossible",多解时应让 n 尽量小。
Sample Input
120 1 210
Sample Output
Case 1: 5 1 Case 2: Impossible Case 3: 7 4
思路:当k==1时一定是Impossible,当k是奇数时,m最小是k-1,n最小是k,仔细一想就知道
当k是偶数时,需要找到m,通过化简原公式可得(m+1)*(m+2)<=k,所以可以判断m*m一定是小于k的,通过m遍历寻找n即可
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
typedef long long ll;
using namespace std;
int k, n, m;
int t = 1;
ll sum;
int flag;
int main() {
while (scanf("%d", &k) != EOF) {
flag = 0;
if(k==1) printf("Case %d: Impossible\n", t++);
else if (k % 2 == 1)printf("Case %d: %d %d\n", t++,k, k - 1);
else {
for (ll i = 1; i*i <= k; i++) {//m
sum = 1;
for (ll j = i + 1;; j++) {//n
sum = sum*j;
if (sum == k) {
printf("Case %d: %lld %lld\n", t++, j, i);
flag = 1;
break;
}
if (sum > k) {
break;
}
}
if (flag == 1)break;
}
if(!flag) printf("Case %d: Impossible\n", t++);
}
}
system("pause");
return 0;
}