HDU 4345 Permutation(数学题,记忆化搜索)

Permutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 291    Accepted Submission(s): 168

Problem Description There is an arrangement of N numbers and a permutation relation that alter one arrangement into another.

For example, when N equals to 6 the arrangement is 123456 at first. The replacement relation is 312546 (indicate 1->2, 2->3, 3->1, 4->5, 5->4, 6->6, the relation is also an arrangement distinctly).

After the first permutation, the arrangement will be 312546. And then it will be 231456.

In this permutation relation (312546), the arrangement will be altered into the order 312546, 231456, 123546, 312456, 231546 and it will always go back to the beginning, so the length of the loop section of this permutation relation equals to 6.

Your task is to calculate how many kinds of the length of this loop section in any permutation relations.  

 

Input Input contains multiple test cases. In each test cases the input only contains one integer indicates N. For all test cases, N<=1000.  

 

Output For each test case, output only one integer indicates the amount the length of the loop section in any permutation relations.  

 

Sample Input 1 2 3 10  

 

Sample Output 1 2 3 16  

 

Source
2012 Multi-University Training Contest 5  

 

Recommend zhuyuanchen520    

/*
HDU 4345 Permutation
循环节的长度为各独立置换环长度的最小公倍数。问题即求相加和为N的正整数的最小公倍数的可能数。
由于1不影响最小公倍数,问题转化为相加小于等于N的若干正整数的最小公倍数的可能数。
如果这些正整数包含大于一个质因子,只会使得正整数的和更大。
因而问题再次转化为相加小于等于N的若干质数的最小公倍数的可能数。
N<1000,于是可递推得,标程用记忆化搜索实现的。


*/
#include<stdio.h>
#include<string.h>
const int MAXN=1100;
int prime[MAXN+1];
int getPrime()//得到小于等于MAXN的素数,prime[0]存放的是个数
{
    memset(prime,0,sizeof(prime));
    for(int i=2;i<=MAXN;i++)
    {
        if(!prime[i]) prime[++prime[0]]=i;
        for(int j=1;j<=prime[0]&&prime[j]<=MAXN/i;j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0)break;
        }
    }
    return prime[0];
}
long long dp[MAXN][MAXN];

long long DP(int n,int i) //和小于等于n,从第i个素数开始的种类
{
    if(dp[n][i]!=-1)return dp[n][i];//记忆化搜索
    if(prime[i]>n)
    {
        dp[n][i]=1;
        return dp[n][i];
    }
    int k=0;
    dp[n][i]=0;
    while(k<=n)
    {
        dp[n][i]+=DP(n-k,i+1);
        if(k==0)k=prime[i];
        else k*=prime[i];
    }
    return dp[n][i];
}
int main()
{
    getPrime();
    memset(dp,-1,sizeof(dp));
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        printf("%I64d\n",DP(n,1));
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/08/2629080.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞