zoukankan      html  css  js  c++  java
  • BZOJ 1025: [SCOI2009]游戏

    1025: [SCOI2009]游戏

    Time Limit: 1 Sec  Memory Limit: 162 MB

    Submit: 2471  Solved: 1615

    [Submit][Status][Discuss]

    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为最小公倍数,那么有唯一分解可以得到lcm=p1^k1*p2^k2*…*pn^kn,当这些数分别为p1^k1,p2^k2…pn^kn时,满足所有数最小公倍数为lcm并且此时所有数的和最小。

    如果对于一个lcm,所有数的和小于n的话,这个lcm一定可以组成,因为剩下的数用1补全即可。

    设f[i][j]为前i种质数,p1^k1+p2^k2+…+pn^kn=j时的方案数,对于每个质数,枚举k即可。

    代码

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<iostream>
    #define LL long long
    using namespace std;
    const int N=1005;
    int n,tot;
    int pri[N],ispri[N];
    LL ans;
    LL f[N][N];
    void prime(){
    	for(int i=2;i<=n;i++){
    		if(!ispri[i])pri[++tot]=i;
    		for(int j=1;j<=tot&&i*pri[j]<=n;j++){
    			ispri[i*pri[j]]=1;
    			if(i%pri[j]==0)break;
    		}
    	}
    }
    int main(){
    	scanf("%d",&n);
    	prime();
    	f[0][0]=1;
    	for(int i=1;i<=tot;i++){
    		for(int j=0;j<=n;j++){
    			f[i][j]=f[i-1][j];
    		}
    		for(int j=pri[i];j<=n;j*=pri[i]){
    			for(int k=0;k<=n-j;k++){
    				f[i][k+j]+=f[i-1][k];
    			}
    		}
    	}
    	for(int i=0;i<=n;i++)ans+=f[tot][i];
    	printf("%lld
    ",ans);
    	return 0;
    }
  • 相关阅读:
    topcoder srm 320 div1
    topcoder srm 325 div1
    topcoder srm 330 div1
    topcoder srm 335 div1
    topcoder srm 340 div1
    topcoder srm 300 div1
    topcoder srm 305 div1
    topcoder srm 310 div1
    topcoder srm 315 div1
    如何统计iOS产品不同渠道的下载量?
  • 原文地址:https://www.cnblogs.com/chezhongyang/p/7690578.html
Copyright © 2011-2022 走看看