SP1870 MKLABELS - Making Labels
解题思路
从样例可以找到规律:
- ,。
- ,。
- ,。
- ,。
- ,。
因此可以得出公式:。
也就是输出 (), 时输出 。
证明
由 Cayley 公式: 个标号顶点构成的树有 棵。
证明:每棵树唯一对应一个长度为 、值域 的序列,序列总数 ,因此树的数量相同。
复杂度分析
- 时间复杂度:。
- 空间复杂度:。
代码实现
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans;
int ksm(int a,int b){
int r=1;
while(b){
if(b&1) r=r*a;
a=a*a;
b>>=1;
}
return r;
}
signed main(){
while(scanf("%lld",&n)==1&&n){
ans++;
printf("Case %lld, N = %lld, # of different labelings = %lld\n",ans,n,ksm(n,max(0LL,n-2)));
}
return 0;
}