NBUT 1643 阶乘除法

来源:互联网 发布:arp状态 windows 编辑:程序博客网 时间:2024/04/29 23:19
阶乘除法Crawling in process...Crawling failedTime Limit:5000MS    Memory Limit:65535KB     64bit IO Format:
SubmitStatus Practice NBUT 1643 uDebug

Description

Input

Output

Sample Input

Sample Output

Hint

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

1201210

Sample Output

Case 1: 5 1Case 2: ImpossibleCase 3: 7 4

分析:显然除了1之外,其他的数大师possible的,而却k必然可以分解为(m+1)*(m+2)...(n-1)*n,这样就好办了,可以找出除了1之外的k的所有因子(包括其本身),然后从小到大看是否存在满足(m+1)*(m+2)...(n-1)*n=k的m和n。

代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int y[1000000], cnt;void f(int k){cnt = 0;for (int i = 2; i*i <= k; ++i)if (k%i == 0){y[cnt++] = i;if (i*i != k)y[cnt++] = k / i;}y[cnt++] = k;}int main(){int cas = 0, k;while (cin >> k){if (k == 1){printf("Case %d: Impossible\n",++cas);continue;}int n, m;f(k);sort(y, y + cnt);for (int i = 0; i < cnt; ++i){m = y[i] - 1;n = y[i];int t = k;while (t != 1){if (t%n == 0){t /= n;n++;}elsebreak;}if (t == 1)break;}printf("Case %d: %d %d\n", ++cas, n - 1, m);}return 0;}

0 0