標籤:style blog for 2014 div c++ amp size
A.水題,輸出圖形
B.水題
C.機率題
/*m, n最大數為k的總數為 k^n - (k-1)^n所以最大數為k的期望為 (k^n - (k-1)^n) / (m^n)*/#include<bits/stdc++.h>using namespace std;int main(){ int n, m; int i, j; scanf("%d%d", &m, &n); double ans = m; for(i=1; i<=m-1; ++i) { double x = i*1.0/m; ans = ans - pow(x, n); } printf("%.12f\n", ans); return 0;}
D. 狀態壓縮DP
#include<bits/stdc++.h>using namespace std;int pri[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};int dp[105][1<<16];int p[105][1<<16];int mask[60]; //mask[i]已二進位形式表示是否含有pri[j]int a[105];int main(){ int n, i, j, k; for(i=1; i<60; ++i) for(j=0; j<16; ++j) if(i%pri[j]==0) mask[i] |= 1<<j; scanf("%d", &n); for(i=1; i<=n; ++i) scanf("%d", &a[i]); memset(dp, 0x3f, sizeof dp ); dp[0][0] = 0; for(i=1; i<=n; ++i) { for(j=0; j< (1<<16); ++j) for(k=1; k<60; ++k) if( (mask[k]&j)==0) { if(dp[i][j|mask[k]]>dp[i-1][j] + abs(a[i]-k)) { dp[i][j|mask[k]] = dp[i-1][j] + abs(a[i]-k); p[i][j|mask[k]] = k; } } } vector<int> ans; for(int i=n, m=min_element(dp[i], dp[i]+(1<<16)) - dp[i]; i>0; m ^= mask[p[i][m]], --i) ans.push_back(p[i][m]); for(i=1; i<=n; ++i) printf("%d ", ans[n-i]); return 0;}