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。

Output

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

Sample Input

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

Sample Output

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

HINT

 

【数据规模和约定】

100%的数据,满足 1 <= N <= 1000 。

 
由置换的基本性质可得,所需行数为所有循环节长度的lcm。
考虑唯一分解定理:n=∏pi^ci,我们在ci上做文章。
设f[i][j]表示前i个质因子,用了j个元素构成循环节的方案数。
O(N^2)
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}
typedef long long ll;
const int maxn=1010;
int vis[maxn],pri[maxn],cnt;
void gen(int n) {
	rep(i,2,n) {
		if(!vis[i]) pri[++cnt]=i;
		rep(j,1,cnt) {
			if(i*pri[j]>n) break;
			vis[i*pri[j]]=1;
			if(i%pri[j]==0) break;
		}
	}
}
int n;
ll f[maxn][maxn];
int main() {
	gen(n=read());f[0][0]=1;
	rep(i,1,cnt) rep(j,0,n) {
		f[i][j]+=f[i-1][j];
		for(int k=pri[i];k<=n-j;k*=pri[i]) f[i][j+k]+=f[i-1][j];
	}
	ll ans=0;
	rep(i,0,n) ans+=f[cnt][i];
	printf("%lld\n",ans);
	return 0;
}

  

 
posted @ 2016-04-05 19:38  wzj_is_a_juruo  阅读(204)  评论(0编辑  收藏  举报