[codeforces]Goodbye_2015

来源:互联网 发布:软件搬家 编辑:程序博客网 时间:2024/06/01 03:58

D - New Year and Ancient Prophecy

time limit per test2.5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Limak is a little polar bear. In the snow he found a scroll with the ancient prophecy. Limak doesn’t know any ancient languages and thus is unable to understand the prophecy. But he knows digits!

One fragment of the prophecy is a sequence of n digits. The first digit isn’t zero. Limak thinks that it’s a list of some special years. It’s hard to see any commas or spaces, so maybe ancient people didn’t use them. Now Limak wonders what years are listed there.

Limak assumes three things:

Years are listed in the strictly increasing order;
Every year is a positive integer number;
There are no leading zeros.
Limak is going to consider all possible ways to split a sequence into numbers (years), satisfying the conditions above. He will do it without any help. However, he asked you to tell him the number of ways to do so. Since this number may be very large, you are only asked to calculate it modulo 109 + 7.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — the number of digits.

The second line contains a string of digits and has length equal to n. It’s guaranteed that the first digit is not ‘0’.

Output
Print the number of ways to correctly split the given sequence modulo 109 + 7.

Sample test(s)
input
6
123434
output
8
input
8
20152016
output
4
Note
In the first sample there are 8 ways to split the sequence:

“123434” = “123434” (maybe the given sequence is just one big number)
“123434” = “1” + “23434”
“123434” = “12” + “3434”
“123434” = “123” + “434”
“123434” = “1” + “23” + “434”
“123434” = “1” + “2” + “3434”
“123434” = “1” + “2” + “3” + “434”
“123434” = “1” + “2” + “3” + “4” + “34”
Note that we don’t count a split “123434” = “12” + “34” + “34” because numbers have to be strictly increasing.

In the second sample there are 4 ways:

“20152016” = “20152016”
“20152016” = “20” + “152016”
“20152016” = “201” + “52016”
“20152016” = “2015” + “2016”
题目大意:给定一个长整数,将其分解成若干部分之后,组成的序列满足严格递增总共的序列个数。
解题思路:无法想到用什么状态表示dp,看完题解之后才发现用怎样的状态表示dp,dp[i][j]表示以(i,j)组成的最后一个数字,满足严格递增的序列个数。
数字(a,b-1)和(b, c)
当(b - 1) - a < c - b 即 a > 2b-c-1时满足(a,b-1)组成的数字小于(b, c)
即dp[i][j] = sum {dp[a][i-1] | a from 2b-c to i-1 };
计算其总和的时候如果单纯用循环相加则其复杂度需要o(n^3)
用前缀总和来计算sum = pref[i-1][i-1]-pref[a][i-1]
// pref[a][b] = sum(dp[1..a][b])
int pref[nax][nax];
每次计算dp[i][j]的同时更新pref[i][j] = pref[i-1][j] + dp[i][j];
当(b - 1) - a == c - b, 比较两个数字的大小,题解给了两种方法,一种用哈希函数,一种用动态规划,动态规划的方法nxt[i][j]表示以i为起点,以j为起点的两个数字算起前缀相同的个数, 其方程为nxt[i][j] = nxt[i+1][j+1] + 1
比较的时候只需比较s[i+nxt[i][j]]和s[j+nxt[i][j]]的大小,就能得出两位数字哪个大哪个小

#include <cstdio>#include <algorithm>#include <cstring>using std::max;char s[5010];int dp[5010][5010];int nxt[5010][5010];int summ[5010][5010];const int module = 1e9 + 7;int cmp(int a, int b, int c) {    if (nxt[a][b] >= b-a) {        return 0;    } else {        if (s[a+nxt[a][b]-1] < s[b+nxt[a][b]-1])            return 1;        else            return 0;    }}int main() {    int d, a;    scanf("%d", &d);    scanf("%s", s);    memset(dp, 0, sizeof(dp));    memset(nxt, 0, sizeof(nxt));    memset(summ, 0, sizeof(summ));    for (int i = 0;i <= d; i++) {        dp[1][i] = 1;        summ[1][i] = 1;    }    for (int i = d; i >= 1; i--) {        for (int j = d; j > i; j--) {            if (s[i-1] == s[j-1])                nxt[i][j] = nxt[i+1][j+1] + 1;        }    }    for (int j = 2; j <= d; j++) {        for (int i = 2; i <= j; i++) {            a = max(1, 2*i-j);            if (s[i-1] == '0') {                dp[i][j] = 0;                summ[i][j] += summ[i-1][j];                continue;            }            dp[i][j] = (summ[i-1][i-1]-summ[a-1][i-1]) % module;            if (dp[i][j] < 0)                dp[i][j] += module;            if (a > 1) {                int flag = cmp(a-1, i, j);                if (flag == 1) {                    dp[i][j] = (dp[i][j] + dp[a-1][i-1]) %module;                }            }            summ[i][j] += (summ[i-1][j] + dp[i][j]) % module;        }    }    int sum = 0;    for (int i = 1; i <= d; i++) {        sum = (sum + dp[i][d]) % module;    }    printf("%d\n", sum);    return 0;}
0 0
原创粉丝点击