hdu 4345 Permutation(数论+dp)

题目链接

Permutation

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


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
 

Author
HIT
 

Source


题意:

求和为n的一些数的最小公倍数有多少种。


参考题解


题解:

循环节的长度为各独立置换环长度的最小公倍数。问题即求相加和为N的正整数的最小公倍数的可能数。
由于1不影响最小公倍数,问题转化为相加小于等于N的若干正整数的最小公倍数的可能数。
如果这些正整数包含大于一个质因子,只会使得正整数的和更大。
因而问题再次转化为相加小于等于N的若干质数的最小公倍数的可能数。


记忆化搜索写法上面的参考题解有。

递推写法:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=1000+10;
int prime[maxn];
void init()
{
    for(int i=2;i<maxn;i++)
    {
        if(!prime[i])
        {
            prime[++prime[0]] = i;
        }
        for(int j=1;(j<=prime[0])&&(i*prime[j]<maxn);j++)
        {
            prime[prime[j]*i] = 1;
            if(i%prime[j]==0) break;
        }
    }
}
ll d[maxn][200];  //d[i][j]表示和小于等于i,素数种类为前j种的方法数
int main()
{
    init();
    rep(i,0,maxn) d[i][0]=1;
    rep(i,1,prime[0]+1) d[0][i]=1;
    rep(i,1,maxn)
    {
        for(int j=1;j<=prime[0];j++)
        {
            d[i][j]=d[i][j-1];
            if(prime[j]>i) continue;
            int p=prime[j];
            while(i>=p)
                d[i][j]+=d[i-p][j-1],p*=prime[j];
        }
    }
    int n;
    while(~scanf("%d",&n))
    {
        printf("%lld\n",d[n][prime[0]]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值