Hdu 4345 Permutation

Permutation

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


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
 

Recommend
zhuyuanchen520
 

解题思路

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

以上解题思路的转化对于解该题十分重要

接下来就用01背包求解方案数

不过需要注意,背包的物品为除1以外的因子只有1个质数的数(该质数可有多个)

248163927525等等。。。。

另外,该题需用64位的整数求解,int溢出


#include<stdio.h>
#include<string.h>

__int64 num[1001];
int pri[1001];
int count;

void cre_pri()
{
	int i,j;
	for(i=2;i<=1000;i++)
	{
		if(!num[i])
		{
			pri[count++]=i;
			for(j=i*i;j<=1000;j+=i)
			{
				num[j]=1;
			}
		}
	}
}

int main(void)
{
	int i,j;
	cre_pri();
	memset(num,0,sizeof(num));
	num[0]=1;
	__int64 tmp;
	for(i=0;i<count;i++)
	{
		for(j=1000;j>=pri[i];j--)
		{
			tmp=pri[i];
			while(tmp<=1000)
			{
				if(j>=tmp)
					num[j]+=num[j-tmp];
				tmp*=pri[i];
			}
		}
	}
//	for(i=0;i<=50;i++)
//		printf("%d  %d\n",i,num[i]);
	for(i=1;i<=1000;i++)
	{
		num[i]=num[i-1]+num[i];
	}
	int n;
	while(scanf("%d",&n)==1)
	{
		printf("%I64d\n",num[n]);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值