NC19014. Lucky Coins
描述
输入描述
The first line is the number of test cases. For each test case, the first line contains an integer k representing the number of kinds. Each of the following k lines describes a kind of coins, which contains an integer and a real number representing the number of coins and the probability that the coins come up heads after tossing. It is guaranteed that the number of kinds is no more than 10, the total number of coins is no more than 1000000, and the probabilities that the coins come up heads after tossing are between 0.4 and 0.6.
输出描述
For each test case, output a line containing k real numbers with the precision of 6 digits, which are the probabilities of each kind of coins that will be lucky.
示例1
输入:
3 1 1000000 0.5 2 1 0.4 1 0.6 3 2 0.4 2 0.5 2 0.6
输出:
1.000000 0.210526 0.473684 0.124867 0.234823 0.420066
C++14(g++5.4) 解法, 执行用时: 18ms, 内存消耗: 476K, 提交时间: 2018-10-10 20:39:50
#include<bits/stdc++.h> #define ll long long //#define int long long #define inf (1LL<<61) #define fi first #define se second #define pb push_back #define pa pair<int,int> #define mkp(a,b) make_pair(a,b) const int N=1e3+10; const int mod=998244353; using namespace std; double ksm(double a,ll k){double ans=1; while(k){if(k&1) ans=ans*a; a=a*a;k/=2; }return ans; } double dp[11][N],d[11][N],p[11]; int a[11]; int32_t main() { //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%lf",&a[i],&p[i]); if(n==1) {puts("1.000000"); continue; } for(int i=1;i<=n;i++) { for(int j=1;j<N;j++) { dp[i][j]=ksm(1-ksm(p[i],j),a[i]); d[i][j]=1-dp[i][j]; } } for(int i=1;i<=n;i++) { double ans=0; for(int j=1;j<N-1;j++) { double tmp=d[i][j]-d[i][j+1]; for(int k=1;k<=n;k++) (i!=k)&&(tmp*=dp[k][j]); ans+=tmp; } printf("%.6f%c",ans," \n"[i==n]); } } return 0; } /* */
C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 360K, 提交时间: 2018-10-07 18:49:28
#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int N=100; double d[11][N],l[11][N]; int n; double qpow(double a,int b){ double r=1; while(b){ if(b&1)r=r*a; a=a*a; b>>=1; } return r; } int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&n); int a; double p,k; for(int i=0;i<n;i++){ scanf("%d%lf",&a,&p); k=1; for(int j=0;j<N;j++){ k*=p; d[i][j]=qpow(1-k,a); l[i][j]=1-d[i][j]; } } if(n==1){ printf("1.000000\n"); continue; } for(int i=0;i<n;i++){ double sum=0; for(int j=0;j<N-1;j++){ double t=l[i][j]-l[i][j+1]; for(int k=0;k<n;k++) if(k!=i)t*=d[k][j]; sum+=t; } printf("%lf ",sum); } printf("\n"); } }