NC17668. All in
描述
输入描述
The first line contains an integer, which is n.
The second line contains the results of contests, which are 0s and 1s.
0 means team A wins, and 1 means team A loses(team B wins)
It is guaranteed this is a legal process.
Once the number of 0 or 1 reaches n, there will be no more input.
1 <= n <= 100000
输出描述
You should output the bet for each contest in a new line.
The number of output is the same as the second line of input.
示例1
输入:
2 0 1 1
输出:
4 4 8
说明:
In fact, for n = 2, the bets are always 4 4 8, no matter what the outcome of the matches.示例2
输入:
3 1 1 0 0 1
输出:
12 12 8 16 32
说明:
示例3
输入:
3 0 1 0 0
输出:
12 12 16 16
说明:
示例4
输入:
2 1 1
输出:
4 4
说明:
Team A wins the first two matches. There won't be a third match.C++14(g++5.4) 解法, 执行用时: 96ms, 内存消耗: 7764K, 提交时间: 2018-08-16 16:38:33
#include<bits/stdc++.h> #define maxn 234035 using namespace std; typedef long long ll; const ll M=1000000007; ll n,m,nf[maxn],f[maxn],k,p2[maxn],r,x,y,o; ll pow_(ll x,ll y){ ll rt=1; while (y){ if (y&1) rt=rt*x%M; x=x*x%M; y>>=1; } return rt; } ll C(ll x,ll y){ return f[x+y]*nf[x]%M*nf[y]%M; } int main(){ f[0]=nf[0]=p2[0]=1; for (int i=1;i<maxn;i++) f[i]=f[i-1]*i%M; for (int i=1;i<maxn;i++) nf[i]=nf[i-1]*pow_(i,M-2)%M; for (int i=1;i<maxn;i++) p2[i]=p2[i-1]*2%M; cin >> n; r=1; x=n-1; y=n-1; n++; while (true){ printf("%lld\n",C(x,y)*p2[r]%M); scanf("%lld",&o); if (o) x--; else y--; r++; if (x<0||y<0) return 0; } }
C++11(clang++ 3.9) 解法, 执行用时: 52ms, 内存消耗: 3936K, 提交时间: 2020-04-03 15:53:02
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mod=1e9+7; const int N=1e5+5; ll a,b,ans; ll inv[2*N]; int n,x; ll cal(ll x,ll y) { ll ret=1; for(int i=1;i<=y;++i) ret=ret*(x-i+1)%mod*inv[i]%mod; return ret; } int main() { scanf("%d",&n); inv[1]=1; for(int i=2;i<2*n;++i) inv[i]=(mod-mod/i)*inv[mod%i]%mod; ans=cal(2*n-2,n-1)*2%mod; a=b=0; while(a<n&&b<n) { printf("%lld\n",ans); scanf("%d\n",&x); ans=ans*inv[2*n-a-b-2]%mod*2%mod; if(!x) ++a,ans=ans*(n-a)%mod; else ++b,ans=ans*(n-b)%mod; } }