标签:main ret car .com play res eof http name
#include<iostream> #include<cstdio> #include<ctime> #include<cstdlib> #define maxn 301 using namespace std; int n,a[maxn],q[maxn],cnt[maxn]; long long sz[maxn]; bool vis[maxn]; int gcd(int x,int y){ if(y==0)return x; else return gcd(y,x%y); } void dfs(int pos,int now){ if(clock()>=980){ puts("0.000000000 1.000000000"); exit(0); } if(now==1){ if((pos&1)==0)cnt[pos]++; return; } for(int i=1;i<=n;i++){ if(!vis[i]){ vis[i]=1; dfs(pos+1,gcd(now,a[i])); vis[i]=0; } } } int main(){ //freopen("Cola.txt","r",stdin); freopen("cards.in","r",stdin);freopen("cards.out","w",stdout); scanf("%d",&n); sz[1]=n; for(int i=2;i<=n;i++)sz[i]=sz[i-1]*(n-i+1); for(int i=1;i<=n;i++)scanf("%d",&a[i]); dfs(0,0); double ans=0; for(int i=2;i<=n;i+=2) ans+=(double)cnt[i]/(double)sz[i]; printf("%.9lf ",ans); puts("1.000000000"); }
#include<iostream> #include<cstdio> #include<cstring> #define maxn 100010 #define mod 1000000007 using namespace std; int n,q[maxn],fa[maxn],sz[maxn],w[maxn],cut[maxn],ans,map[maxn]; int num,head[maxn],mx[maxn],vis[maxn]; struct node{ int from,to; }E[maxn]; struct Node{ int to,pre; }e[maxn]; int find(int x){ if(x==fa[x])return x; else return fa[x]=find(fa[x]); } void dfs(int x,int father,int stx,int sty,int wnow){ for(int i=head[x];i;i=e[i].pre){ int to=e[i].to; if(to==father)continue; if(mx[to]<mx[sty]+wnow+w[to]){ mx[to]=mx[sty]+wnow+w[to]; map[to]=map[sty]; dfs(to,x,stx,sty,wnow+w[to]); } } } void connect(int x,int y){ int f1=find(x),f2=find(y); int now=mx[x]+mx[y]; fa[f2]=f1; if(now>sz[f1]&&now>sz[f2]){ sz[f1]=now; sz[f2]=0; } else{ sz[f1]=max(sz[f1],sz[f2]); sz[f2]=0; } dfs(x,x,x,y,w[x]); dfs(y,y,y,x,w[y]); int xx=map[x],yy=map[y]; int maxx=mx[x],maxy=mx[y]; map[map[x]]=yy;map[map[y]]=xx; if(mx[x]<maxy+w[x]){mx[x]=maxy+w[x];map[x]=yy;} if(mx[y]<maxx+w[y]){mx[y]=maxx+w[y];map[y]=xx;} } void Insert(int from,int to){ e[++num].to=to; e[num].pre=head[from]; head[from]=num; e[++num].to=from; e[num].pre=head[to]; head[to]=num; } int qread(){ int i=0; char ch=getchar(); while(ch<‘0‘||ch>‘9‘)ch=getchar(); while(ch<=‘9‘&&ch>=‘0‘){i=i*10+ch-‘0‘;ch=getchar();} return i; } int main(){ freopen("forest.in","r",stdin);freopen("forest.out","w",stdout); //freopen("Cola.txt","r",stdin); n=qread(); q[n]=1; for(int i=1;i<=n;i++){ w[i]=qread(); fa[i]=i,sz[i]=w[i]; mx[i]=w[i]; map[i]=i; q[n]=(1LL*w[i]*q[n])%mod; } for(int i=1;i<n;i++)scanf("%d%d",&E[i].from,&E[i].to); for(int i=1;i<n;i++)scanf("%d",&cut[i]); for(int i=n-1;i>=1;i--){ connect(E[cut[i]].from,E[cut[i]].to); Insert(E[cut[i]].from,E[cut[i]].to); long long ans=1; memset(vis,0,sizeof(vis)); for(int j=1;j<=n;j++){ int now=find(j); if(!vis[now]){ vis[now]=1; ans=ans*sz[now]; if(ans>=mod)ans%=mod; } } q[i]=(int)ans; //for(int j=1;j<=n;j++)printf("%d %d %d\n",j,map[j],mx[j]);puts(""); } for(int i=1;i<=n;i++){ printf("%d\n",q[i]); } return 0; } /* 8 2 3 6 3 9 4 5 6 4 8 3 5 4 1 1 3 1 2 6 5 7 5 2 6 3 1 7 4 5 */
标签:main ret car .com play res eof http name
原文地址:http://www.cnblogs.com/thmyl/p/7685827.html