bzoj1025 [SCOI2009]游戏

Description

  windy学会了一种游戏。对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应。最开始windy把数字按顺序1,2,3,……,N写一排在纸上。然后再在这一排下面写上它们对应的数字。然后又在新的一排下面写上它们对应的数字。如此反复,直到序列再次变为1,2,3,……,N。

如: 1 2 3 4 5 6 对应的关系为 1->2 2->3 3->1 4->5 5->4 6->6
windy的操作如下
1 2 3 4 5 6
2 3 1 5 4 6
3 1 2 4 5 6
1 2 3 5 4 6
2 3 1 4 5 6
3 1 2 5 4 6
1 2 3 4 5 6
这时,我们就有若干排1到N的排列,上例中有7排。现在windy想知道,对于所有可能的对应关系,有多少种可能的排数。

Input

  包含一个整数N,1 <= N <= 1000

Output

  包含一个整数,可能的排数。

Sample Input

【输入样例一】
3
【输入样例二】
10

Sample Output

【输出样例一】
3
【输出样例二】
16

正解:置换+背包。

这题要求对于一个数列的任意一种置换,有多少种循环节。

很显然,答案就是把$n$个数的置换进行循环分解,每个循环大小的$lcm$。

那么就是说对于$n$,我们需要把它分成若干数的和,求出这些数的$lcm$的方案数。

这里不加证明地给出一个结论,答案就是使得$p1^{a1}+p2^{a2}+...+pn^{an}<=n$的方案数,其中$p$为质数,很显然这个结论是对的。

那么我们筛出$[1,n]$的所有质数,做一遍背包就行了,具体实现看代码吧。

 1 //It is made by wfj_2048~
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <complex>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <cmath>
10 #include <queue>
11 #include <stack>
12 #include <map>
13 #include <set>
14 #define inf (1<<30)
15 #define il inline
16 #define RG register
17 #define ll long long
18 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
19 
20 using namespace std;
21 
22 int prime[1010],vis[1010],n,cnt;
23 ll f[1010][1010],ans;
24 
25 il int gi(){
26     RG int x=0,q=1; RG char ch=getchar();
27     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
28     if (ch=='-') q=-1,ch=getchar();
29     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
30     return q*x;
31 }
32 
33 il void sieve(){
34     for (RG int i=2;i<=n;++i){
35     if (!vis[i]) prime[++cnt]=i;
36     for (RG int j=1,k;j<=cnt;++j){
37         k=i*prime[j]; if (k>n) continue;
38         vis[k]=1; if (!i%prime[j]) break;
39     }
40     }
41     return;
42 }
43 
44 il void work(){
45     n=gi(),sieve(),f[0][0]=1;
46     for (RG int i=1;i<=cnt;++i){
47     for (RG int j=0;j<=n;++j) f[i][j]=f[i-1][j];
48     for (RG int j=prime[i];j<=n;j*=prime[i])
49         for (RG int k=0;k+j<=n;++k) f[i][k+j]+=f[i-1][k];
50     }
51     for (RG int i=0;i<=n;++i) ans+=f[cnt][i];
52     printf("%lld
",ans); return;
53 }
54 
55 int main(){
56     File("game");
57     work();
58     return 0;
59 }
原文地址:https://www.cnblogs.com/wfj2048/p/6932930.html