首页 > 其他 > 详细

HDU 4345 Permutation dp

时间:2015-08-21 01:48:16      阅读:272      评论:0      收藏:0      [点我收藏+]

Permutation

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


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
 
题意:
    有N个元素的一个集合经过K次置换能变回原来的集合,求k的个数。
题解:
  dp[n][i]:表示前i种质数表示的数和为n的ways
  dp[n][i]=dp[n][i-1]+dp[n-j*prim[i]]][i]        j(0,1,2......n/prim[i]); 
技术分享
///1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
typedef __int64 ll;
using namespace std;
#define inf 10000000
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<0||ch>9)
    {
        if(ch==-)f=-1;
        ch=getchar();
    }
    while(ch>=0&&ch<=9)
    {
        x=x*10+ch-0;
        ch=getchar();
    }
    return x*f;
}
int gcd(int a,int b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
//***************************************************************
ll dp[1002][1002];
ll prim[1002];
ll hashs[1002];
ll cnt;
ll dfs(ll n,ll p)
{
    if(dp[n][p])return dp[n][p];
    if(n<prim[p])
    {
        dp[n][p]=1;
        return 1;
    }
    dp[n][p]=dfs(n,p+1);
    ll tmp=prim[p];
    while(tmp<=n)
    {
        dp[n][p]+=dfs(n-tmp,p+1);
        tmp*=prim[p];
    }
    return dp[n][p];

}
int main()
{ll n;
    cnt=0;
    for(ll i=2;i<=1001;i++)
    {
        if(hashs[i]==0)
        {
            prim[cnt++]=i;
            for(ll j=i+i;j<=1000;j+=i)hashs[j]=1;
        }

    }
    memset(dp,0,sizeof(dp));
    while(scanf("%I64d",&n)!=EOF){
        printf("%I64d\n",dfs(n,0));
    }
        return 0;
}
代码狗

 

 

HDU 4345 Permutation dp

原文:http://www.cnblogs.com/zxhl/p/4746634.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!