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;
    }
    代码狗
  • 相关阅读:
    VSCode配置Python开发环境
    图像特征——边缘
    关于相机内参中的焦距fx和fy
    摄影变换和仿射变换
    为什么要引入齐次坐标
    链表一
    从小问题看懂链表
    类与对象
    排序一
    数组
  • 原文地址:https://www.cnblogs.com/zxhl/p/4746634.html
Copyright © 2011-2022 走看看