[NOI2014]起床困难综合症 题解
题目地址:ė …
May all the beauty be blessed.
题目地址:HDUOJ:Problem – 4352
#define xhxj (Xin Hang senior sister(学姐))
If you do not know xhxj, then carefully reading the entire description is very important.
As the strongest fighting force in UESTC, xhxj grew up in Jintang, a border town of Chengdu.
Like many god cattles, xhxj has a legendary life:
2010.04, had not yet begun to learn the algorithm, xhxj won the second prize in the university contest. And in this fall, xhxj got one gold medal and one silver medal of regional contest. In the next year’s summer, xhxj was invited to Beijing to attend the astar onsite. A few months later, xhxj got two gold medals and was also qualified for world’s final. However, xhxj was defeated by zhymaoiing in the competition that determined who would go to the world’s final(there is only one team for every university to send to the world’s final) .Now, xhxj is much more stronger than ever,and she will go to the dreaming country to compete in TCO final.
As you see, xhxj always keeps a short hair(reasons unknown), so she looks like a boy( I will not tell you she is actually a lovely girl), wearing yellow T-shirt. When she is not talking, her round face feels very lovely, attracting others to touch her face gently。Unlike God Luo’s, another UESTC god cattle who has cool and noble charm, xhxj is quite approachable, lively, clever. On the other hand,xhxj is very sensitive to the beautiful properties, “this problem has a very good properties”,she always said that after ACing a very hard problem. She often helps in finding solutions, even though she is not good at the problems of that type.
Xhxj loves many games such as,Dota, ocg, mahjong, Starcraft 2, Diablo 3.etc,if you can beat her in any game above, you will get her admire and become a god cattle. She is very concerned with her younger schoolfellows, if she saw someone on a DOTA platform, she would say: “Why do not you go to improve your programming skill”. When she receives sincere compliments from others, she would say modestly: “Please don’t flatter at me.(Please don’t black).”As she will graduate after no more than one year, xhxj also wants to fall in love. However, the man in her dreams has not yet appeared, so she now prefers girls.
Another hobby of xhxj is yy(speculation) some magical problems to discover the special properties. For example, when she see a number, she would think whether the digits of a number are strictly increasing. If you consider the number as a string and can get a longest strictly increasing subsequence the length of which is equal to k, the power of this number is k.. It is very simple to determine a single number’s power, but is it also easy to solve this problem with the numbers within an interval? xhxj has a little tired,she want a god cattle to help her solve this problem,the problem is: Determine how many numbers have the power value k in [L,R] in O(1)time.
For the first one to solve this problem,xhxj will upgrade 20 favorability rate。
定义一个数的内部LIS长度为这个数拆成一个个数字这个序列的LIS长度,给出区间[l, r],求区间内内部LIS长度为k的数的个数。
输入格式:
First a integer T(T<=10000),then T lines follow, every line has three positive integer L,R,K.(0<L<=R<2^63-1 and 1<=K<=10).
输出格式:
For each query, print “Case #t: ans” in a line, in which t is the number of the test case starting from 1 and ans is the answer.
输入样例#1:
1 123 321 2
输出样例#1:
Case #1: 139
思路参考HDU 4352 XHXJ’s LIS(数位dp&状态压缩) – CSDN博客,感谢原作者。
首先这个题我们可以想到数位DP(人生第一个数位DP题),但是数位DP怎么来处理LIS是个很棘手的问题。
我们考虑O(n \log n)求LIS的方法,是在外部维护了一个当前LIS的数组,我们如果在这里用这个数组,会发现LIS的长度不会大于10,LIS的数组显然是可以状态压缩做的。这样,如果进行一些预处理,求LIS的复杂度可以是O(1)的。
设计状态dp[len][S][k]为枚举到长度为len,S为目前LIS数组里的元素情况,要求的LIS长度为k的数字数量。考虑采取记忆化搜索,我们可以向后枚举当前位置可以填充哪个数字。当填充完毕后,检查LIS是否等于k,并返回,这是整个搜索的终点。由于我们询问区间,势必会对答案产生一定的限制,如果当前搜索受到限制,那么搜索的结果不应该被保存。还要特别处理一下前导0之类的。
实现细节参考代码注释吧。
// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF
: *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
LL dp[20][1 << 10][11];
int dis[20], cnt[1 << 10], nxt[10][1 << 10];
// 这里是计算每个状态后插入一个数的状态
inline int calnxt(int s, int num) {
for(int i = num; i <= 9; i++) {
if(s & (1 << i)) {
return (s ^ (1 << i)) | (1 << num);
}
}
return s | (1 << num);
}
inline void init() {
memset(dp, -1, sizeof(dp));
// 把每个状态的LIS长度处理出来
for(int i = 0; i < (1 << 10); i++) {
cnt[i] = 0;
for(int j = 0; j < 10; j++) {
if(i & (1 << j)) cnt[i]++;
nxt[j][i] = calnxt(i, j);
}
}
}
// s表示状态,lim表示当前是否受限,zero表示当前是否在前导0里
inline LL dfs(int k, int len, int s, bool lim, bool zero) {
if(len < 0) return cnt[s] == k;
if(!lim && dp[len][s][k] != -1) return dp[len][s][k];
LL res = 0;
int limm = lim ? dis[len] : 9;
for(int i = 0; i <= limm; i++) {
// zero产生的影响是状态不会发生改变
res += dfs(k, len - 1, (zero && i == 0) ? s : nxt[i][s], lim && i == limm,
zero && i == 0);
}
if(!lim) dp[len][s][k] = res;
return res;
}
inline LL work(LL n, LL k) {
// 先处理出来限制条件
int pos = 0;
while(n) {
dis[pos++] = n % 10;
n /= 10;
}
return dfs(k, pos - 1, 0, true, true);
}
int T;
LL l, r, k;
int main() {
T = readint();
init();
for(int ii = 1; ii <= T; ii++) {
l = readint();
r = readint();
k = readint();
// 这里我们选择把0~l-1和0~r的都求出来然后减掉
printf("Case #%d: %lld\n", ii, work(r, k) - work(l - 1, k));
}
return 0;
}
题目地址:POJ:3734 — Blocks
Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.
给一排砖块涂色,一共有红蓝绿黄4种颜色,要求最后红色和绿色砖块数一定是偶数,求合法的涂色方案数。
输入格式:
The first line of the input contains an integer T(1≤T≤100), the number of test cases. Each of the next T lines contains an integer N(1≤N≤10^9) indicating the number of blocks.
输出格式:
For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007.
输入样例#1:
2 1 2
输出样例#1:
2 6
我们用dp[i][0/1/2]表示涂到第i个砖块,当前红绿砖块分别全是奇数、一奇一偶、全是偶数时合法的方案数。我们可以通过下面的式子来转移。
\begin{matrix} dp[i][0] = 2dp[i - 1][0] + dp[i - 1][1] \\ dp[i][1] = 2dp[i - 1][0] + 2dp[i - 1][1] + 2dp[i - 1][2] \\ dp[i][2] = dp[i - 1][1] + 2dp[i - 1][2] \end{matrix}
我一看,人群裆中……不对,这个好像可以用矩阵来表示转移。表示出来是下面这个样子
\begin{pmatrix} dp[i][0] \\ dp[i][1] \\ dp[i][2] \end{pmatrix} = \begin{pmatrix} dp[i-1][0] \\ dp[i-1][1] \\ dp[i-1][2] \end{pmatrix} \times \begin{pmatrix} 2 & 1 & 0 \\ 2 & 2 & 2 \\ 0 & 1 & 2 \end{pmatrix}
那显然这个可以矩阵快速幂算一下,把后面那个转移矩阵n次方一下就好了。
// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline int readint() {
register int res = 0, neg = 1;
char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
const int MO = 10007;
struct Matrix {
int m[3][3];
};
inline void mul(Matrix &a, Matrix b) {
Matrix res;
memset(res.m, 0, sizeof(res.m));
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++) {
res.m[i][j] += a.m[i][k] * b.m[k][j];
res.m[i][j] %= MO;
}
}
}
a = res;
}
inline Matrix fpow(Matrix x, int k) {
if(k == 1) return x;
Matrix res = fpow(x, k >> 1);
mul(res, res);
if(k & 1) {
mul(res, x);
}
return res;
}
int T, n;
Matrix mat, def;
inline void init() {
memset(mat.m, 0, sizeof(mat.m));
mat.m[0][0] = 2;
mat.m[0][1] = 1;
mat.m[1][0] = mat.m[1][1] = mat.m[1][2] = 2;
mat.m[2][1] = 1;
mat.m[2][2] = 2;
memset(def.m, 0, sizeof(def.m));
def.m[0][0] = def.m[1][1] = def.m[2][2] = 1;
}
int main() {
T = readint();
while(T--) {
n = readint();
init();
mat = fpow(mat, n);
mul(def, mat);
printf("%d\n", def.m[0][0]);
}
return 0;
}
题目地址:HDUOJ:Problem – 5181
Now you have a stack and n numbers 1,2,3,…,n. These n numbers are pushed in the order and popped if the number is at the top of the stack. You can read the sample to get more details.
This question is quite easy. Therefore I must give you some limits.
There are m limits, each is expressed as a pair <A,B> means the number A must be popped before B.
Could you tell me the number of ways that are legal in these limits?
I know the answer may be so large, so you can just tell me the answer mod 1000000007(10^9+7).
一个由1,2,…,n组成的排列,顺次插入栈中,你可以选择在任意时刻弹出栈顶元素,现在有m个要求,要求x必须在y之前出栈,问有多少种合法的出栈顺序。
输入格式:
The first line contains an integer T(about 5),indicating the number of cases.
Each test case begins with two integers n(1≤n≤300) and m(1≤m≤90000).
Next m lines contains two integers A and B(1≤A≤n,1≤B≤n)
(P.S. there may be the same limits or contradict limits.)
输出格式:
For each case, output an integer means the answer mod 1000000007.
输入样例#1:
5 1 0 5 0 3 2 1 2 2 3 3 2 2 1 2 3 3 3 1 2 2 3 3 1
输出样例#1:
1 42 1 2 0
The only legal pop-sequence of case 3 is 1,2,3.
The legal pop-sequences of case 4 are 2,3,1 and 2,1,3.
我们首先简化问题,不考虑限制,那我们设计状态dp[i][j]表示区间[i, j]内的方案数,枚举区间内最后一个弹出的元素k,那么有下面的方程
dp[i][j] = \sum_{k=i}^j (dp[i][k - 1] * dp[k + 1][j])
有了限制应该怎么办?我们先分析限制对我们转移的影响,假如有限制<A, B>:
如果A<B,包含[A, B]的区间[i, j],枚举的最后弹出元素为k在(A, B]之间时候显然成立,而如果出现A、B在k的同侧时这个问题会在更小的区间内被解决,不是我们现在考虑的。但是如果k=A时,A就比B晚弹出,这是显然不行的。结论:对于A<B的情况,k≠A。
如果A=B,这种条件并没办法成立,答案直接是0了。
如果A>B,包含[A, B]的区间[i, j],枚举的最后弹出元素为k不能在(B, A]内。
朴素地思考,我们可以枚举k的时候逐个条件判断一下。但是这样复杂度就成了O(n^3m),已经无法接受了。
那咋整啊?我们回头来看看一个限制对什么样的区间产生影响。这样的区间[i, j]肯定满足1 \leq i \leq \min(A, B), \max(A, B) \leq j \leq n。我们把区间的两个端点看成二维平面上的一个点,那么这个影响其实是对一个矩形产生的。对于给矩形打标记这种事情,自然可以使用二维差分处理。差分完了以后前缀和求一下,就能查单点了。我们开n个二维平面,枚举k来标记每个k在哪些区间里不能转移即可。这个题巧妙之处就在于把二维差分的思想用进去了。
最后的时间复杂度O(n^3+nm),空间复杂度O(n^3)。
// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXN = 305, MO = 1000000007;
int T, n, m, a, b;
LL dp[MAXN][MAXN];
int s[MAXN][MAXN][MAXN];
inline void add(int xa, int ya, int xb, int yb, int dimm) {
s[xa][ya][dimm]++;
s[xb + 1][yb + 1][dimm]++;
s[xb + 1][ya][dimm]--;
s[xa][yb + 1][dimm]--;
}
int main() {
T = readint();
while(T--) {
memset(dp, 0, sizeof dp);
memset(s, 0, sizeof s);
n = readint();
m = readint();
bool flag = false;
for(int i = 1; i <= m; i++) {
a = readint();
b = readint();
if(a == b) flag = true;
if(a < b) {
add(1, b, a, n, a);
} else {
for(int j = b + 1; j <= a; j++) {
add(1, a, b, n, j);
}
}
}
if(flag) {
printf("0\n");
continue;
}
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
s[i][j][k] += s[i - 1][j][k] + s[i][j - 1][k] - s[i - 1][j - 1][k];
}
}
}
for(int i = 1; i <= n; i++) {
dp[i][i] = dp[i][i - 1] = 1;
}
dp[n + 1][n] = 1;
for(int len = 2; len <= n; len++) {
for(int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1;
for(int k = i; k <= j; k++) {
if(!s[i][j][k]) {
dp[i][j] = (dp[i][j] + dp[i][k - 1] * dp[k + 1][j] % MO) % MO;
}
}
}
}
printf("%lld\n", dp[1][n]);
}
return 0;
}