1. 程式人生 > >UVA 1575 - Factors

UVA 1575 - Factors

color i++ () def tps 理解 || 如果 void

令 f(k)=n 表示 有 n 種方式,可以把正整數 k 表示成幾個素數的乘積的形式。
例 10=2*5=5*2,所以 f(10)=2
給出 n,求最小的 k

搜索

從最小的質數開始枚舉選幾個

假設前i-1個種質數用了k個,有sum種方案,第i種質數選a個,

那麽前i種質數的方案就有sum*C[k+a][a]

可以理解原來有k個位置,又加了a個位置,有a個數可以放在任意位置

所以前i種的每一種方案都變成C[k+a][a]種

枚舉每個質數選幾個時,如果上一個質數選了k個,那麽這一個質數最多選k個

假設這個質數選了k+1個,那麽顯然上一個質數選k+1個,這個選k個更優

註意整數類型上限

(copy原地址)

屠龍寶刀點擊就送

#include<cstdio>
typedef unsigned long long LL;
int p[21]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
LL ans,n;
LL C[70][70];
void solve(int num,int lim,LL tot,LL now,int last)
{
    if(now>ans) return;
    if(tot==n) { ans=now; return
; } if(tot>n || num>20) return; LL t=1; for(int i=1;i<=lim;i++) { t*=p[num]; if(now>=ans/t) return; solve(num+1,i,tot*C[last+i][i],now*t,last+i); } } int main() { C[0][0]=1; for(int i=1;i<70;i++) { C[i][0]=1;
for(int j=1;j<=i;j++) C[i][j]=C[i-1][j-1]+C[i-1][j]; } while(scanf("%lld",&n)!=EOF) { if(n==1) { printf("1 2\n"); continue; } ans=(LL)1<<63; solve(1,63,1,1,0); printf("%lld ",n);printf("%lld\n",ans); } return 0; }

UVA 1575 - Factors