zoukankan      html  css  js  c++  java
  • HDU 4345 Permutation dp

                                                  Permutation

    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的方案数。
        例如:n=10,
        有如下方案:1,2,3,4,5,6,7,8,9,2*5,4*3,2*7,2*9,3*5,4*5,3*7;  一共16种
    题解:
       不难发现,就是求每个置换集合的lcm.
       我们可以用dp来求解:
       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
    ",dfs(n,0));
        }
            return 0;
    }
    代码狗
  • 相关阅读:
    50个网页常用小代码
    web前端题目(持续更新)
    一步步构建大型网站架构(转)
    CentOS下配置node.js
    ajax文件上传
    test
    文件上传input简便美化方案
    String.match()与RegExp.exec()
    ie7下zindex问题
    javascript将数组插入到另一个数组中
  • 原文地址:https://www.cnblogs.com/zxhl/p/4746634.html
Copyright © 2011-2022 走看看