首页 > 代码库 > uvalive 6396 数论 世界决赛的题

uvalive 6396 数论 世界决赛的题

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=589&problem=4407&mosmsg=Submission+received+with+ID+1528513


Factors
The fundamental theorem of arithmetic states that every integer greater than 1 can be uniquely represented as a product of one or more primes. While unique, several arrangements of the prime factors
may be possible. For example:
10 = 2 * 5 20 = 2 * 2 * 5
= 5 * 2 = 2 * 5 * 2
= 5 * 2 * 2
Let f (k) be the number of di erent arrangements of the prime factors of k. So f (10) = 2 and
f (20) = 3.
Given a positive number n, there always exists at least one number k such that f (k) = n. We want
to know the smallest such k.
Input
The input consists of at most 1000 test cases, each on a separate line. Each test case is a positive
integer n < 2
63
.
Output
For each test case, display its number n and the smallest number k > 1 such that f (k) = n. The
numbers in the input are chosen such that k < 2
63
.
Sample Input
1
2
3
105
Sample Output
1 2
2 6
3 12
105 720

其实没judge 因为uvalive的评测有问题  我自己测的数据反正跟之前AC的结果一样。。。。

但是我二逼的是----因为不能judge,我就拿别的AC代码对拍作为judge, 不合法的情况输出应该是设定的INF,然后我设定的最大值跟AC代码设定的INF不一样,我以为自己错了,就debug了好几天,今天发现,卧槽,,,,其实我的可能一直都是对的啊,,,当然以后uvalive好了再说

思路就是反素数了,具体看教程http://blog.csdn.net/u011026968/article/details/38784687

然后写的话,就很容易了,就是跟http://blog.csdn.net/u011026968/article/details/38787473  很想,基本直接copy

我的代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const int INF = 100000000;

const int maxc=100;
//const int MOD = 1e9+7;
ll C[maxc][maxc];
void calC(){  // C(n,k),n个数里选k个
    int i,j;
    for(int i=0;i<maxc;i++) C[i][i]=C[i][0]=1LL;
    for(int i=2;i<maxc;i++)
        for(j=1;j<i;j++)
            C[i][j]=(C[i-1][j]+C[i-1][j-1]);//%MOD;
}
const int SIZE = 21;
int p[SIZE] = {2,3,5,7,11,
                13,17,19,23,29,
                31,37,41,43,47,
                53,59,61,67,71,73};//7;53;59;61;67;71;73
ll ans,n;
void dfs(ll tmp, int dep, int num, ll ret,int cc)
{
    ///////////////////////
    //printf("tmp=%lld dep=%d num=%d ret=%lld cc=%d\n",tmp,dep,num,ret,cc);
    ////////////////////////
    if(ret == n)
        ans=min(ans,tmp);
    if(ret>=n || dep>SIZE-1 )return;
    ll tt=1;
    for(int i=1;i<num;i++)
    {
        tt*=p[dep];

        /////////////////
        //printf("n=%lld le=%lld %lld ri=%lld %lld\n",n,n/ret,C[cc+i][i],ans/tt , tmp);
        /////////////////////
        //if(n/ret > C[cc+i][i] || ans/tt < tmp)break;//
        if(ans/tt < tmp)break;
        //if(cc+i>64)break;
        dfs(tmp*tt,dep+1,i+1,ret*C[cc+i][i],cc+i);

    }
}

int main()
{
    //IN("uva6396.txt");
    calC();
    while(~scanf("%lld",&n))
    {
        ans=ll_INF;
        if(n == 1)ans=2;
        else dfs(1, 0, 65, 1, 0);
        printf("%lld %lld\n",n,ans);
    }
    return 0;
}

AC代码两份:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define FOR(i, x, y) for(int i = x; i <= y; i++)
#define oo 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll N;
ll C[65][65];
int P[20];
bool isPrime(int n)
{
	FOR(i, 2, sqrt(n))
	if(n % i == 0) return 0;
	return 1;
}
void init()
{
	FOR(i, 0, 64)
	{
		C[i][0] = 1;
		FOR(j, 1, i)
			C[i][j] = C[i-1][j] + C[i-1][j-1];
	}
	FOR(i, 2, 64)
	if(isPrime(i))
		P[++P[0]] = i;
}
ll ans;
int a[65];
int sum[65];
void dfs(int dep, ll s)
{
	if(s == 1)
	{
		double temp = 0;
		FOR(i, 1, dep - 1)
			temp += a[i] * log(P[i]);
		ll t = 1;
		if(temp < 63 * log(2))
		{
			FOR(i, 1, dep - 1)
			FOR(j, 1, a[i])
				t *= P[i];
			ans = min(ans, t);
		}
	}
	if(s <= 1) return;
	FOR(i, 1, a[dep - 1])
	{
		a[dep] = i;
		sum[dep] = sum[dep-1] + i;
		if(sum[dep] > 64) break;
		if(C[sum[dep]][i] && s % C[sum[dep]][i] == 0)
			dfs(dep + 1, s / C[sum[dep]][i]);
	}
}
ll solve()
{
	ans = 9223372036854775807L;
	a[0] = 64;
	dfs(1, N);
	if(N == 1) ans = 2;
	return ans;
}
int main()
{
	init();
	while(scanf("%lld", &N) != EOF)
	{
		printf("%lld %lld\n", N, solve());
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <map>
using namespace std;
#define IN(s) freopen(s,"r",stdin)
#define maxn 1009
int p[]={1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
typedef unsigned long long ull;
ull mod = 1000000007;
const ull INFULL=(ull)(-1LL);
map<ull,ull> ans;
ull n[maxn];
bool safemul(ull &c,int x)
{

	if((c*x)%mod!=((c%mod)*x)%mod)
		return false;
	c*=x;
	return true;
}
ull qpow(ull a,ull n,ull mod)
{
	ull b=1;
	a%=mod;
	while(n)
	{
		if(n&1) b*=a,b%=mod;
		a*=a,a%=mod;
		n>>=1;
	}
	return b;
}
void dfs(int t,int s,int sum,ull a,ull b,ull c)
{
	ull tmp = b*qpow(a,mod-2,mod)%mod;
	if(ans.find(tmp)!=ans.end()){
		ull &x = ans[tmp];
		if(x>c) x=c;
	}
	if(t>15)
		return;
	int i;
	for(i=1;i<=s;i++){
		b*=i+sum,b%=mod;
		a*=i,a%=mod;
		if(!safemul(c,p[t])) return;
		dfs(t+1,i,sum+i,a,b,c);
	}
}
int main()
{
	//freopen("D.in","r",stdin);
	//freopen("D.out","w",stdout);
	//IN("uva6396.txt");
	int p=0;
	while(cin>>n[++p]){
		ans[n[p]%mod]=INFULL;
	}
	p--;
	dfs(1,64,0,1,1,1);
	int i;
	for(i=1;i<=p;i++){
		if(n[i]==1){
			cout<<"1 2"<<endl;
			continue;
		}
		cout<<n[i]<<" "<<ans[n[i]%mod]<<endl;
	}
	return 0;
}


uvalive 6396 数论 世界决赛的题