2015 Multi-University Training Contest 9
A. Expression(理解原理 区间DP)
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<queue> #include<cmath> #define maxn 105 #define mod 1000000007 using namespace std; long long dp[maxn][maxn]; long long A[maxn],C[maxn][maxn]; char operations[maxn]; int a[maxn],n; /*初始化从0到maxn的阶乘模mod*/ void init() { int i,j; A[0] = 1; for(i = 1;i < maxn; i++) { A[i] = (A[i-1]*i)%mod; } C[0][0] = 1; for(i=1; i<=100; i++) { C[i][0] = 1; for(j=1; j<=i; j++) C[i][j] = (C[i-1][j-1] + C[i-1][j])%mod; } } void solve() { int len,l,r,k; for(l = 1;l <= n;l ++) dp[l][l] = a[l]; for(len = 2;len <= n;len ++) { for(l = 1;l + len - 1 <= n; l ++) { long long t; r = l + len -1; for(k = l;k < r;k ++) { if(operations[k] == '+'){ t = (dp[l][k]*A[r-k-1] + dp[k+1][r]*A[k-l])%mod; } else if(operations[k] == '-'){ t = (dp[l][k]*A[r-k-1] - dp[k+1][r]*A[k-l])%mod; } else{ t = (dp[l][k]* dp[k+1][r])%mod; } dp[l][r] = (dp[l][r] + t* C[r-l-1][k-l])%mod; } } } } int main() { init(); while(cin >> n) { memset(dp, 0, sizeof(dp)); int i,j; for(i = 1;i <= n;i ++){ cin >> a[i]; } for(i = 1;i < n;i ++){ cin >> operations[i]; } solve(); cout << (dp[1][n]+mod)%mod << endl; //此段代码借鉴的,为什么? } return 0; }
小结:此题看过题解后,思路是对的,但是对取模的机制和组合数的求法不是很熟练,需要加强。
Travelling Salesman Problem
#include <iostream> #include<cstdio> #define maxn 105 #define black 1 #define white 0 using namespace std; int n,m; long long sum,min; int map[maxn][maxn],color[maxn][maxn]; //color[][] = 0/1,1代表白色,0代表黑色 void init() { int i ,j; bool tmp = true; for(i = 1;i <= n;i ++) { for(j = 1;j <= m;j ++) { if(tmp) color[i][j] = black; else color[i][j] = white; tmp = !tmp; } } } int main() { while(scanf("%d %d",&n,&m)!=EOF) { init(); sum = 0; int i,j; for(i = 1;i <= n;i ++) { for(j = 1;j <= m;j ++) { scanf("%d",&map[i][j]); sum += map[i][j]; } } /*行数n为奇数*/ if(n%2!=0) { printf("%lld ",sum); for(i = 1;i <= n;i ++) { if(i%2 != 0) { for(j = 1;i < m;i ++) printf("R"); } else { printf("D"); for(j= 1;j < m;j ++) printf("L"); } } } /*行数为偶数,但列数为奇数*/ else if(m%2 != 0) { printf("%lld ",sum); for(i = 1;i <= m;i ++) { if(i%2 != 0) { for(j = 1;i < n;i ++) printf("D"); } else { printf("L"); for(j= 1;j < n;j ++) printf("U"); } } } /*行列都为偶数*/ else { } } return 0; }