Chess Queen
组合数
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define MAXN 100010
using namespace std;
int n,m;
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
while(scanf("%d%d",&n,&m)==2)
{
if(n==0&&m==0) break;
long long cur_ans=0;
if(n>m) swap(n,m);
for(int i=1;i<=n-1;i++) cur_ans+=1ll*i*(i-1);
// printf("cur_ans=%lld
",cur_ans);
printf("%lld
",1ll*(cur_ans)*4+1ll*n*m*(n+m-2)+1ll*2*(m-n+1)*n*(n-1));
}
return 0;
}
例题2 Triangle Counting
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 1000010
using namespace std;
int n;
long long dp[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
dp[3]=0;
for(int i=4;i<=1000000;i++)
dp[i]=dp[i-1]+(1ll*(i-1)*(i-2)/2-(i-1)/2)/2;
while(scanf("%d",&n)==1)
{
if(n<3) break;
printf("%lld
",dp[n]);
}
return 0;
}
例题3 Cheerleaders
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define MAXN 500
#define mod 1000007
using namespace std;
int T,n,m,k,kase;
int c[MAXN+10][MAXN+10];
inline void init()
{
for(int i=0;i<=MAXN;i++) c[i][0]=c[i][i]=1;
for(int i=2;i<=MAXN;i++)
for(int j=1;j<i;j++)
c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
init();
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
int cur_ans=0;
kase++;
for(int i=0;i<(1<<4);i++)
{
int op=0,nn=n,mm=m;
if(i&(1<<0)) op++,nn--;
if(i&(1<<1)) op++,nn--;
if(i&(1<<2)) op++,mm--;
if(i&(1<<3)) op++,mm--;
if(op&1) cur_ans=(cur_ans+mod-c[nn*mm][k])%mod;
else cur_ans=(cur_ans+c[nn*mm][k])%mod;
}
printf("Case %d: %d
",kase,cur_ans);
}
return 0;
}
例题4 Exploring Pyramids
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 310
#define mod (int)1e9
using namespace std;
int dp[MAXN][MAXN],done[MAXN][MAXN];
char s[MAXN];
inline int solve(int l,int r)
{
// printf("l=%d r=%d
",l,r);
if(l==r) return 1;
if(s[l]!=s[r]) return 0;
if(done[l][r]) return dp[l][r];
done[l][r]=1;
for(int k=l+2;k<=r;k++)
if(s[l]==s[k])
dp[l][r]=(dp[l][r]+1ll*solve(l+1,k-1)*solve(k,r)%mod)%mod;
return dp[l][r];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
while(scanf("%s",s+1)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(done,0,sizeof(done));
printf("%d
",solve(1,strlen(s+1)));
}
return 0;
}
例题5 Investigating Div-Sum Property
数位DP
(dp[i][m1][m2])表示i位,数位之和为m1,数值为m2的种类数。
(dp[i][m1][m2]=sum{dp[i-1][(m1-x)mod k][(m2-x*10^{i-1})mod k]})
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 110
using namespace std;
int a,b,cnt,k,T;
int num[MAXN];
long long dp[MAXN][MAXN][MAXN];
long long fpow[15]={0,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000};
inline long long calc(int len,int m1,int m2,int limit)
{
if(len<=0)
{
if(m1==0&&m2==0) return 1;
else return 0;
}
if(!limit&&dp[len][m1][m2]>=0) return dp[len][m1][m2];
int maxx=(limit==1)?num[len]:9;
long long cur_ans=0;
for(int i=0;i<=maxx;i++)
{
int cur=m2-i*pow(10,len-1);
cur=(cur+((-cur)/k+1)*k)%k;
cur_ans+=calc(len-1,(m1-i+k)%k,cur%k,limit==1&&i==maxx);
}
if(!limit) dp[len][m1][m2]=cur_ans;
return cur_ans;
}
inline long long solve(int x)
{
cnt=0;
memset(num,0,sizeof(num));
memset(dp,-1,sizeof(dp));
while(x)
{
num[++cnt]=x%10;
x/=10;
}
long long ans=calc(cnt,0,0,1);
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
freopen("ce.out","w",stdout);
#endif
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&a,&b,&k);
if(k>81) printf("0
");
else printf("%lld
",solve(b)-solve(a-1));
}
return 0;
}
例题6 Glenbow Museum
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 3010
using namespace std;
int n,cnt;
long long c[MAXN][MAXN];
inline void init()
{
for(int i=0;i<=MAXN-10;i++) c[i][0]=c[i][i]=1;
for(int i=1;i<=MAXN-10;i++)
for(int j=1;j<=MAXN-10;j++)
c[i][j]=c[i-1][j-1]+c[i-1][j];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
init();
while(scanf("%d",&n)==1&&n)
{
long long ans;
if((n&1)||n<4) ans=0;
else ans=c[(n+4)/2][4]+c[(n+4)/2-1][4];
printf("Case %d: %lld
",++cnt,ans);
}
return 0;
}
例题6 Series-Parallel Networks
例题7 Always an integer
例题8 GCD - Extreme (II)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<ctime>
#define MAXN 1000010
#define int long long
using namespace std;
int cnt,kase;
int ci[MAXN],xi[MAXN];
char s[MAXN];
inline int calc(int x,int mod)
{
int ans=0;
for(int i=1;i<=cnt;i++)
{
int cur_ans=1;
for(int j=1;j<=ci[i];j++)
cur_ans=1ll*cur_ans*x%mod;
ans=(ans+cur_ans*xi[i])%mod;
}
if(ans==0) return true;
else return false;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
while(scanf("%s",s))
{
if(s[0]=='.') break;
int f=1,cur_sum=0,pos=1;
cnt=0;
if(s[1]!='-') s[0]='+';
for(int i=0,len=strlen(s);i<len;)
{
if(s[i]=='-'||s[i]=='+')
{
if(s[i]=='-') f=-1;
else f=1;
i++;
cur_sum=0;
while(s[i]>='0'&&s[i]<='9')
{
cur_sum=cur_sum*10+s[i]-'0';
i++;
}
if(cur_sum==0) cur_sum=1;
xi[++cnt]=cur_sum*f;
if(s[i]=='n') i++;
if(s[i]=='^')
{
i++,cur_sum=0;
while(s[i]>='0'&&s[i]<='9')
{
cur_sum=cur_sum*10+s[i]-'0';
i++;
}
ci[cnt]=cur_sum;
}
else if(s[i]!='^') ci[cnt]=1;
continue;
}
if(s[i]=='/') {pos=i;break;}
i++;
}
cur_sum=0;
for(int i=pos+1,len=strlen(s);i<len;i++) cur_sum=cur_sum*10+s[i]-'0';
// printf("cur_sum=%d
",cur_sum);
// for(int i=1;i<=cnt;i++) printf("xi[%d]=%d
",i,xi[i]);
// for(int i=1;i<=cnt;i++) printf("ci[%d]=%d
",i,ci[i]);
bool flag=true;
for(int i=1;i<=max(1ll,ci[1]+1);i++)
{
if(calc(i,cur_sum)==false)
{
flag=false;
break;
}
}
if(flag==true) printf("Case %d: Always an integer
",++kase);
else printf("Case %d: Not always an integer
",++kase);
}
return 0;
}
例题9 Code Feat
例题10 Emoogle Grid
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<set>
#define ll long long
#define MAXN 100010
#define MOD 100000007
using namespace std;
ll n,k,b,r,maxx,T,kase;
ll x[MAXN],y[MAXN];
set<pair<int,int> >se;
inline ll fpow(ll x,ll y,ll mod)
{
ll cur_ans=1;
while(y)
{
if(y&1){cur_ans=cur_ans*x%mod;}
x=x*x%mod;
y>>=1;
}
return cur_ans;
}
inline ll bsgs(ll y,ll z,ll mod)
{
z%=mod,y%=mod;
if(z==1) return 0;
map<long long,int>M;
ll m=(ll)sqrt(mod)+1;
for(int i=0,t=z;i<m;i++,t=1ll*t*y%mod) M[t]=i;
for(int i=1,tt=fpow(y,m,mod),t=tt;i<m;i++,t=1ll*tt*t%mod)
if(M.count(t))
return i*m-M[t];
return -1;
}
inline ll calc()
{
ll cur_ans=0;
for(int i=1;i<=b;i++)
if(x[i]!=maxx&&!se.count(make_pair(x[i]+1,y[i])))
cur_ans++;
cur_ans+=n;
for(int i=1;i<=b;i++)
if(x[i]==1)
cur_ans--;
return (fpow(k,cur_ans,MOD)*fpow(k-1,maxx*n-cur_ans-b,MOD)%MOD)%MOD;
}
inline ll solve()
{
ll pre_ans=calc();
if(pre_ans==r) return maxx;
ll tmp=n;
for(int i=1;i<=b;i++)
if(x[i]==maxx)
tmp--;
ll cur_ans=(fpow(k-1,tmp,MOD)*fpow(k,n-tmp,MOD))%MOD;
cur_ans=pre_ans*cur_ans%MOD;
if(cur_ans==r) return maxx+1;
return bsgs(fpow(k-1,n,MOD),r*fpow(cur_ans,MOD-2,MOD)%MOD,MOD)+maxx+1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld%lld%lld",&n,&k,&b,&r);
se.clear();
maxx=1;
for(int i=1;i<=b;i++)
{
scanf("%lld%lld",&x[i],&y[i]);
se.insert((make_pair)(x[i],y[i]));
maxx=max(maxx,x[i]);
}
printf("Case %lld: %lld
",++kase,solve());
}
return 0;
}
例题11 Playing With Stones
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 110
using namespace std;
int T,n;
long long a[MAXN];
inline long long sg(long long x)
{
if(x&1) return sg(x/2);
else return x/2;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
long long cur_ans=0;
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<=n;i++) cur_ans^=sg(a[i]);
if(cur_ans) printf("YES
");
else printf("NO
");
}
return 0;
}
例题12 Treblecross
SG函数方案输出
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define MAXN 210
using namespace std;
int T,len;
int sg[MAXN];
char s[MAXN],cur[MAXN];
vector<int>vec;
inline int calc(int x)
{
if(x<0) return 0;
if(sg[x]!=-1) return sg[x];
int done[MAXN];
memset(done,0,sizeof(done));
for(int i=1;i<=x;i++)
done[calc(x-i-2)^calc(i-3)]=1;
for(int i=0;;i++)
if(done[i]==0)
{
return sg[x]=i;
}
}
inline bool solve(int x)
{
if(s[x]=='X') return false;
for(int i=1;i<=len;i++) cur[i]=s[i];
cur[x]='X';
for(int i=1;i<=len-2;i++)
if(cur[i]=='X'&&cur[i+1]=='X'&&cur[i+2]=='X') return true;
for(int i=1;i<=len-1;i++)
if(cur[i]=='X'&&cur[i+1]=='X') return false;
for(int i=1;i<=len-2;i++)
if(cur[i]=='X'&&cur[i+2]=='X') return false;
int ans=0,flag=0,w=2,cnt=-1;
for(int i=len;i>=1;i--)
if(cur[i]=='X')
{
cnt=i;
break;
}
for(int j,i=1;i<=len;i++)
{
if(i>cnt) w=0;
if(cur[i]=='X') flag=2;
if(cur[i]!='.') continue;
for(j=i;j<=len&&cur[j]=='.';j++);
ans^=calc(j-i-flag-w);
i=j-1;
}
if(ans==0) return true;
else return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
freopen("ce.out","w",stdout);
#endif
memset(sg,-1,sizeof(sg));
scanf("%d",&T);
while(T--)
{
scanf("%s",(s+1));
len=strlen((s+1));
vec.clear();
for(int i=1;i<=len;i++)
{
if(solve(i)==true)
vec.push_back(i);
}
if(vec.size())
{
printf("WINNING
");
for(int i=0;i<vec.size()-1;i++)
printf("%d ",vec[i]);
printf("%d
",vec[vec.size()-1]);
}
else printf("LOSING
");
}
return 0;
}
例题13 Tribles
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#define MAXN 1010
using namespace std;
int t;
double f[MAXN],p[MAXN];
int main()
{
scanf("%d",&t);
for(int cnt=1;cnt<=t;cnt++)
{
int n,k,m;
scanf("%d%d%d",&n,&k,&m);
memset(f,0,sizeof(f));
for(int i=0;i<n;i++) scanf("%lf",&p[i]);
f[1]=p[0];
for(int i=2;i<=m;i++)
for(int j=0;j<n;j++)
f[i]+=p[j]*pow(f[i-1],j);
printf("Case #%d: %.7lf
",cnt,pow(f[m],k));
}
return 0;
}
例题14 Joining with Friend
几何概率+巧妙的分类讨论(其实这种讨论方法我是从别人那里抄的)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 100010
using namespace std;
int T,kase;
double t1,t2,s1,s2,ans;
inline double calc(double w)
{
double cur_ans=0.0;
if(s2>t2+w&&s1>t1+w)
{
double cur=max(0.0,t2+w-s1);
cur_ans+=ans-cur*cur*0.5;
}
else if(s2>t2+w)
{
double cur=s2-(t1+w)+s2-(t2+w);
cur_ans+=cur*(t2-t1)*0.5;
}
else if(s1>t1+w)
{
double cur=(s1-w)-t1+(s2-w)-t1;
cur_ans+=(s2-s1)*cur*0.5;
}
else
{
double cur=max(0.0,s2-w-t1);
cur_ans+=cur*cur*0.5;
}
if(w<0) cur_ans=ans-cur_ans;
// printf("cur_ans=%lf
",cur_ans);
return cur_ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
double w;
scanf("%lf%lf%lf%lf%lf",&t1,&t2,&s1,&s2,&w);
// printf("%lf %lf %lf %lf %lf
",t1,t2,s1,s2,w);
++kase;
ans=0.0;
double cur_ans=0.0;
ans=fabs(t1-t2)*fabs(s1-s2);
cur_ans+=calc(w)+calc(-w);
// printf("end cur_ans=%lf
",cur_ans);
printf("Case #%d: %.8lf
",kase,1-cur_ans/ans);
}
return 0;
}
例题15 Expect the Expected
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define eps 1e-8
#define MAXN 110
using namespace std;
int T,n,a,b,kase;
double p;
double dp[MAXN][MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
scanf("%d/%d%d",&a,&b,&n);
kase++;
p=1.0*a/b;
// printf("p=%lf
",p);
for(int i=0;i<=100;i++)
for(int j=0;j<=100;j++)
dp[i][j]=0.0;
dp[0][0]=1.0;
for(int i=1;i<=n;i++)
{
for(int j=0;j*b<=a*i;j++)
{
dp[i][j]+=dp[i-1][j]*(1.0-p);
if(j) dp[i][j]+=dp[i-1][j-1]*p;
// printf("dp[%d][%d]=%lf
",i,j,dp[i][j]);
}
}
double ans=0.0;
for(int j=0;j*b<=a*n;j++) ans+=dp[n][j];
printf("Case #%d: %d
",kase,(int)(1/ans));
}
return 0;
}
例题16 Race to 1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define eps 1e-8
#define MAXN 110
using namespace std;
int T,n,a,b,kase;
double p;
double dp[MAXN][MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d",&T);
while(T--)
{
scanf("%d/%d%d",&a,&b,&n);
kase++;
p=1.0*a/b;
// printf("p=%lf
",p);
for(int i=0;i<=100;i++)
for(int j=0;j<=100;j++)
dp[i][j]=0.0;
dp[0][0]=1.0;
for(int i=1;i<=n;i++)
{
for(int j=0;j*b<=a*i;j++)
{
dp[i][j]+=dp[i-1][j]*(1.0-p);
if(j) dp[i][j]+=dp[i-1][j-1]*p;
// printf("dp[%d][%d]=%lf
",i,j,dp[i][j]);
}
}
double ans=0.0;
for(int j=0;j*b<=a*n;j++) ans+=dp[n][j];
printf("Case #%d: %d
",kase,(int)(1/ans));
}
return 0;
}
例题18 Arif in Dhaka (First Love Part 2)
旋转:
如果旋转k个的话,相当于(i)和((i+k)%n)在一个循环中,假设t的时候第一次完成了一次循环,所以(tk\%n=0),所以(t_{min}=frac{n}{gcd(n,k)}),所以循环中有t个元素,一共(gcd(n,k))个循环。这些置换的不动点为(sum_{i=0}^{n-1}t^{gcd(i,n)})
翻转:
当n为奇数的时候,对称轴有n条,每条对称轴形成(n-1)/2个长度为2的循环,和1个长度为1的循环。所以一共是(nt^{frac{n+1}{2}})个不动点。
当n为偶数的时候,穿过珠子的对称轴有(n/2)条,形成(n/2-1)个长度为2的循环和1个长度为1的循环;不穿过珠子的对称轴有(n/2)条,形成(n/2)个长度为2的循环。所以不动点的个数为(frac{n}{2}(t^{n/2+1}+t^{n/2}))。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 110
using namespace std;
int n,t;
long long pow[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
while(scanf("%d%d",&n,&t)==2)
{
if(n==0) break;
memset(pow,0,sizeof(pow));
pow[0]=1;
for(int i=1;i<=n;i++) pow[i]=pow[i-1]*t;
long long ans1=0,ans2=0;
for(int i=0;i<n;i++) ans1+=pow[__gcd(n,i)];
if(n&1) ans2=n*pow[(n+1)/2];
else ans2=(n/2)*(pow[n/2+1]+pow[n/2]);
printf("%lld %lld
",ans1/n,(ans1+ans2)/2/n);
}
return 0;
}
然后根据polya定理,除以n即可。
例题19 Leonardo's Notebook
例题20 Find the Permutations
把原来的序列看做一个置换,它可以分解成为循环。一个元素不需要交换,两个元素的循环需要交换1次,n个元素的循环需要交换n-1次,所以该1-n的排列分解成x个循环,就需要n-x次交换。所以我们现在求出来循环的个数就行了。因为循环就等于圆排列,所以我们利用第一类斯特林数求出来圆排列的个数即可。
初始化(dp[1][1]=1)因为1个元素组成1个圆排列的种类数为1.
递推方程(dp[i][j]=dp[i-1][j]+dp[i-1][j-1]*(i-1))
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 21
using namespace std;
int n,k;
unsigned long long dp[MAXN+10][MAXN+10];
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
freopen("ce.out","w",stdout);
#endif
dp[1][1]=1;
for(int i=2;i<=MAXN;i++)
{
for(int j=1;j<=i;j++)
{
dp[i][j]=dp[i-1][j-1];
if(j>0) dp[i][j]+=dp[i-1][j]*(i-1);
}
}
while(scanf("%d%d",&n,&k)==2)
{
if(n==0&&k==0) break;
cout<<dp[n][n-k]<<endl;
}
return 0;
}
例题20 Pixel Shuffle
例题21 Recurrences
例题22 Cellular Automaton
例题23 Back to Kernighan-Ritchie
例题24 Square
例题25 solve It
火柴(uva11375)
用n(1<=n<=2000)根火柴棍能组成多少个非负整数?火柴不必用完,组成的整数不能有前导零(但整数0是允许的)。比如,若你有3根火柴,可以组成1或7,如果有4根,除了可以组成1和7之外,还可以组成4和11.
立方数之和:(uva11137)
输入正整数n(n<=10000),求将n分成若干个正整数的立方和有多少种方法qwq
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 10010
using namespace std;
int n;
long long dp[22][MAXN];
inline void solve()
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<=21;i++)
for(int j=0;j<=MAXN-10;j++)
for(int k=0;j+k*i*i*i<=MAXN-10;k++)
dp[i][j+k*i*i*i]+=dp[i-1][j];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
solve();
while(scanf("%d",&n)!=EOF)
{
printf("%lld
",dp[21][n]);
}
return 0;
}
效率改进版(有点完全背包的思想)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 10010
using namespace std;
int n;
long long dp[22][MAXN];
inline void solve()
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<=21;i++)
{
for(int j=0;j<=MAXN-10;j++)
{
dp[i][j]+=dp[i-1][j];
if(j-i*i*i>=0) dp[i][j]+=dp[i][j-i*i*i];
else continue;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
solve();
while(scanf("%d",&n)!=EOF)
{
printf("%lld
",dp[21][n]);
}
return 0;
}
村民排队(uva11174)
村子里现在有n(1<=n<=40000)个人,有多少种方式可以把它们排成一列,使得没有人排在他父亲前面(有些人的父亲可能不在村子里)?输入n和每个人的父亲编号(村子里的人编号为1-n),输出方案总数除以1e9+7的余数)
带标号连通图计数
统计有n(n<=50)个顶点的连通图有多少个。图的顶点有编号。例如n=3时有4个不同的图。