ACM/ICPC World Finals 2013 D Factors

来源:互联网 发布:淘宝双十一退货 编辑:程序博客网 时间:2024/04/26 12:20

题目大意

定义f(k)=k,给定n求f(k)=n的最小的k

解答

首先将k分解质因数:

k=i=1mpaii

很容易就可以得到:
f(k)=(mi=1ai)!mi=1ai!

对于一个新加的素数:
f(k)=f(k)×(m+1i=1ai)!(mi=1ai)!×am+1!=f(k)×Cam+1m+1i=1ai

然后我们就可以发现我们只要取前若干个质数就可以了,然后我们暴力维护每个n所对应的k(k263),就可以了,事实上时间也不大,一秒完全够了,后面只需要读入并把预处理出来的答案输出即可。

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <algorithm>#include <map>using namespace std;#define LLU unsigned long long intconst LLU oo = 1ULL<<63;LLU yh[100][100];LLU ss[50] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67};map<LLU, LLU> anses;//求组合数void preWork(){    yh[0][0] = 1;    for (int i = 1; i <= 62; i++) {        yh[i][0] = 1;        for (int j = 1; j <= 62; j++) {            if (yh[i-1][j] <= oo && yh[i-1][j-1] <= oo)                yh[i][j] = yh[i-1][j] + yh[i-1][j-1];            else                yh[i][j] = oo;        }    }}//暴力搜索维护答案void work(LLU k, LLU n, LLU count, LLU i){    if (count && (!anses.count(k) || anses[k] > n))        anses[k] = n;    LLU tot = 0;    LLU tmp = n;    while(ss[i] <= oo/tmp) {        tot++;        tmp *= ss[i];        work(k*yh[count+tot][tot], tmp, count+tot, i+1);    }}int main(){    //freopen("factors.in", "r", stdin);    //freopen("factors.out", "w", stdout);    preWork();    work(1, 1, 0, 0);    LLU x;    while (scanf("%I64u", &x) != EOF) {        printf("%I64u %I64u\n", x, anses[x]);    }    return 0;}
0 0