bzoj1925 地精部落

来源:互联网 发布:单片机信号线太长 编辑:程序博客网 时间:2024/04/28 22:55

地精部落

题目背景:

bzoj1925

分析:在网上看到了一种没有搞明白的DP方式(X,跟我自己YY的完全不同,写了一发交上去,还是过掉了,感觉自己的方式还是很好理解的,所以也写出来分析下。

我们定义dp[i][j]表示目前考虑到第i个数,并且当前这个数在前i个数当中排名第j位,注意我这里不是指是多少,而是表示排名,那么状态转移就非常好想了。

然后分别以第二个数大于第一个,和第二个数小于第一个dp两次就OK

Source 

/*created by scarlyw*/#include <cstdio>#include <string>#include <algorithm>#include <cstring>#include <iostream>#include <cmath>#include <cctype>#include <vector>#include <set>#include <queue>inline char read() {static const int IN_LEN = 1024 * 1024;static char buf[IN_LEN], *s, *t;if (s == t) {t = (s = buf) + fread(buf, 1, IN_LEN, stdin);if (s == t) return -1;}return *s++;}///*template<class T>inline void R(T &x) {static char c;static bool iosig;for (c = read(), iosig = false; !isdigit(c); c = read()) {if (c == -1) return ;if (c == '-') iosig = true;}for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;}//*/const int OUT_LEN = 1024 * 1024;char obuf[OUT_LEN], *oh = obuf;inline void write_char(char c) {if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;*oh++ = c;}template<class T>inline void W(T x) {static int buf[30], cnt;if (x == 0) write_char('0');else {if (x < 0) write_char('-'), x = -x;for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;while (cnt) write_char(buf[cnt--]);}}inline void flush() {fwrite(obuf, 1, oh - obuf, stdout);}/*template<class T>inline void R(T &x) {static char c;static bool iosig;for (c = getchar(), iosig = false; !isdigit(c); c = getchar())if (c == '-') iosig = true;for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;}//*/const int MAXN = 4200 + 10;int ans, n, p;int sum[MAXN];int dp[2][MAXN];int main() {R(n), R(p), dp[1][1] = 1;for (int i = 1; i <= n; ++i) sum[i] = 1;for (int i = 2; i <= n; ++i) {for (int j = 1; j <= i; ++j)dp[(i & 1)][j] = (i & 1) ? ((sum[n] - sum[j - 1] + p) % p) : (sum[j - 1]);for (int j = 1; j <= n; ++j) sum[j] = (sum[j - 1] + dp[(i & 1)][j]) % p;}memset(dp, 0, sizeof(dp));ans = sum[n], dp[1][1] = 1;for (int i = 1; i <= n; ++i) sum[i] = 1;for (int i = 2; i <= n; ++i) {for (int j = 1; j <= i; ++j)dp[(i & 1)][j] = (i & 1) ? (sum[j - 1]) : ((sum[n] - sum[j - 1] + p) % p);for (int j = 1; j <= n; ++j) sum[j] = (sum[j - 1] + dp[(i & 1)][j]) % p;}ans = (ans + sum[n]) % p;std::cout << ans;return 0;}


原创粉丝点击