zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    	return 0;
    }
    

      

     
  • 相关阅读:
    iOS
    “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift
    iOS
    iOS
    springboot rabbitmq 死信队列应用场景和完整demo
    LRU
    分布式系统高可用原则
    Java8 Stream 流使用场景和常用操作
    下载安装Zookeeper
    Java8内置的函数式编程接口应用场景和方式
  • 原文地址:https://www.cnblogs.com/wzj-is-a-juruo/p/5356527.html
Copyright © 2011-2022 走看看