M-SOLUTIONS Programming Contest

A.(n-2)*180

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 typedef long long ll;
 7 using namespace std;
 8 
 9 int n;
10 
11 int main(){
12     scanf("%d",&n); printf("%d\n",(n-2)*180);
13     return 0;
14 }
View Code

B.已确定的输局<=7

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 typedef long long ll;
 7 using namespace std;
 8 
 9 int n,x;
10 char s[20];
11 
12 int main(){
13     scanf("%s",s+1); n=strlen(s+1);
14     rep(i,1,n) if (s[i]=='x') x++;
15     if (x>7) puts("NO"); else puts("YES");
16     return 0;
17 }
View Code

C.现只考虑A最后胜的情况,B同理。枚举A赢第n局之前B赢了多少局i,那么若不考虑平局概率则这种情况的发生概率为A^n*B^i*C(n-1+i,i),由期望显然可以得到,一场非平局的出现概率为1-C则期望1/(1-C)会出现一场非平局,共有n+i个非平局,则期望局数为(n+i)/(1-C)。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 typedef long long ll;
 7 using namespace std;
 8 
 9 const int N=2000010,mod=1e9+7;
10 int n,A,B,C,ans,fac[N],inv[N];
11 
12 int ksm(int a,int b){
13     int res=1;
14     for (; b; a=1ll*a*a%mod,b>>=1)
15         if (b & 1) res=1ll*res*a%mod;
16     return res;
17 }
18 
19 int cc(int n,int m){ return n<m ? 0 : 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod; }
20 
21 int main(){
22     scanf("%d%d%d%d",&n,&A,&B,&C); int ii=ksm(100,mod-2);
23     A=1ll*A*ii%mod; B=1ll*B*ii%mod; C=1ll*C*ii%mod;
24     int a=1ll*ksm(A+B,mod-2)*A%mod,b=1ll*ksm(A+B,mod-2)*B%mod;
25     fac[0]=1; rep(i,1,n+n) fac[i]=1ll*fac[i-1]*i%mod;
26     inv[n+n]=ksm(fac[n+n],mod-2); for (int i=n+n; i; i--) inv[i-1]=1ll*inv[i]*i%mod;
27     rep(i,0,n-1){
28         ans=(ans+1ll*ksm(b,i)*ksm(a,n)%mod*cc(n-1+i,i)%mod*ksm(1-C+mod,mod-2)%mod*(n+i))%mod;
29         ans=(ans+1ll*ksm(a,i)*ksm(b,n)%mod*cc(n-1+i,i)%mod*ksm(1-C+mod,mod-2)%mod*(n+i))%mod;
30     }
31     printf("%d\n",ans);
32     return 0;
33 }
View Code

D.能取到最大值=总和-最大点权。于是把c从大到小排序,按DFS序分配即可。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 #define For(i,x) for (int i=h[x],k; i; i=nxt[i])
 7 typedef long long ll;
 8 using namespace std;
 9 
10 const int N=20010;
11 int n,u,v,tot,ans,cnt,c[N],s[N],h[N],to[N<<1],nxt[N<<1];
12 struct E{ int u,v; }e[N];
13 void add(int u,int v){ to[++cnt]=v; nxt[cnt]=h[u]; h[u]=cnt; }
14 bool cmp(int a,int b){ return a>b; }
15 
16 void dfs(int x,int fa){
17     s[x]=c[++tot];
18     For(i,x) if ((k=to[i])!=fa) dfs(k,x);
19 }
20 
21 int main(){
22     scanf("%d",&n);
23     rep(i,2,n) scanf("%d%d",&u,&v),e[i]=(E){u,v},add(u,v),add(v,u);
24     rep(i,1,n) scanf("%d",&c[i]);
25     sort(c+1,c+n+1,cmp); dfs(1,0);
26     rep(i,2,n) ans+=min(s[e[i].u],s[e[i].v]);
27     printf("%d\n",ans);
28     rep(i,1,n) printf("%d ",s[i]);
29     return 0;
30 }
View Code

E.答案等于(x/d)*(x/d+1)*...*(x/d+(n-1))*d^n,这就是个阶乘乘上快速幂。注意特判d=0或x/d~x/d+n-1中出现0的情况。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 4 using namespace std;
 5 
 6 const int mod=1e6+3;
 7 int x,d,n,T,fac[mod+10];
 8 
 9 int ksm(int a,int b){
10     int res=1;
11     for (; b; a=1ll*a*a%mod,b>>=1)
12         if (b & 1) res=1ll*res*a%mod;
13     return res;
14 }
15 
16 int main(){
17     fac[0]=1; rep(i,1,mod-1) fac[i]=1ll*fac[i-1]*i%mod;
18     for (scanf("%d",&T); T--; ){
19         scanf("%d%d%d",&x,&d,&n);
20         if (!d){ printf("%d\n",ksm(x,n)); continue; }
21         x=1ll*x*ksm(d,mod-2)%mod;
22         if (!x || x+n-1>=mod) puts("0");
23             else printf("%lld\n",1ll*fac[x+n-1]*ksm(fac[x-1],mod-2)%mod*ksm(d,n)%mod);
24     }
25     return 0;
26 }
View Code

 

posted @ 2019-06-02 13:00  HocRiser  阅读(169)  评论(0编辑  收藏  举报