Hdu 4345 Permutation

来源:互联网 发布:电脑软件网推荐 编辑:程序博客网 时间:2024/06/01 07:11

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
12310
 

Sample Output
12316
 

Author
HIT
 

Source
2012 Multi-University Training Contest 5
 

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