HDU 4345 Permutation dp

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;
}

代码狗

时间: 2024-10-21 04:49:56

HDU 4345 Permutation dp的相关文章

hdu 2583 permutation 动态规划

Problem Description Permutation plays a very important role in Combinatorics. For example ,1 2 3 4  5 and 1 3 5 4 2 are both 5-permutations. As everyone's known, the number of n-permutations is n!. According to their magnitude relatives ,if we insert

HDU 4917 Permutation

题意: 一个序列p1.p2.p3--pn是由1.2.3--n这些数字组成的  现给出一些条件pi<pj  问满足所有条件的排列的个数 思路: 很容易想到用一条有向的线连接所有的pi和pj  那么就构成了有向无环图(题中说有解所以无环) 又因为pi各不相同  那么题目就变成了有向无环图的拓扑排序的种类数 题目中边数较少  所以可能出现不连通情况  我们先讨论一个连通集合内拓扑排序的种类数 题目中m较小  可以利用状压后的记忆化搜索解决 现在考虑如果知道了A和B两个集合各自的种类数  如果把它们合起

HDU 4917 Permutation 拓扑排序的计数

题意: 一个有n个数的排列,给你一些位置上数字的大小关系.求合法的排列有多少种. 思路: 数字的大小关系可以看做是一条有向边,这样以每个位置当点,就可以把整个排列当做一张有向图.而且题目保证有解,所以只一张有向无环图.这样子,我们就可以把排列计数的问题转化为一个图的拓扑排序计数问题. 拓扑排序的做法可以参见ZJU1346 . 因为题目中点的数量比较多,所以无法直接用状压DP. 但是题目中的边数较少,所以不是联通的,而一个连通块的点不超过21个,而且不同连通块之间可以看做相互独立的.所以我们可以对

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

HDU 4968 (水dp 其他?)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 using namespace std; 7 const int inf = 0x3f3f3f3f; 8 const int MAX = 200+10; 9 double GPA[10],dp1[20][30000],dp2[20][30000

hdu 4123 树形DP+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=4123 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses,

HDU 3853 概率dp

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2337    Accepted Submission(s): 951 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).Homura wants to help he

hdu 1087 简单dp

思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int inf=(0x7f7f7f7f); int main() { int a; int s[10005]; int w[10005];