2015南阳CCPC C - The Battle of Chibi DP

C - The Battle of Chibi

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

Description

Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao‘s army. But all generals and soldiers of Cao Cao were loyal, it‘s impossible to convince any of them to betray Cao Cao.

So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.

Yu Zhou discussed with Gai Huang and worked out N information to be leaked, in happening order. Each of the information was estimated to has ai value in Cao Cao‘s opinion.

Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact M information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N information and just select M of them. Find out how many ways Gai Huang could do this.

Input

The first line of the input gives the number of test cases, T(1≤100). T test cases follow.

Each test case begins with two numbers N(1≤N≤103) and M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then N numbers in a line, the ith number ai(1≤ai≤109) indicates the value in Cao Cao‘s opinion of the ith information in happening order.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the ways Gai Huang can select the information.

The result is too large, and you need to output the result mod by 1000000007(109+7).

Sample Input

2
3 2
1 2 3
3 2
3 2 1

Sample Output

Case #1: 3Case #2: 0

HINT

题意

给你n个数,求长度为m的上升子序列有多少个

题解:

n^3就直接暴力就好了,但是会TLE

于是用树状数组优化到n^2logn就好了

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 1000000005
#define eps 1e-12
#define PI acos(-1.0)
#define LL long long

using namespace std;

const int maxn = 1005;
const int mod = 1000000007;
inline long long read()
{
    long long x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int a[maxn], b[maxn], c[maxn][maxn], n, m, dp[maxn][maxn];

inline void Init()
{
    for (int i = 1; i <= n; i++)
    {
        a[i] = b[i] = 0;
        for (int j = 1; j <= n; j++)
            dp[i][j] = c[i][j] = 0;
    }
    return;
}

inline int Lowbit(int x)
{
    return x & (- x);
}

inline void Update(int l, int pos, int key)
{
    while(pos <= n)
    {
        c[l][pos] = (c[l][pos] + key);
        while(c[l][pos]>=mod)
            c[l][pos] -= mod;
        pos = pos + Lowbit(pos);
    }
    return;
}

inline int Sum(int l, int pos)
{
    int temp = 0;
    while(pos)
    {
        temp = (temp + c[l][pos]);
        while(temp >= mod)temp -= mod;
        pos = pos - Lowbit(pos);
    }
    return temp;
}

int main()
{
    int T, cas = 0;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        Init();
        for (int i = 1; i <= n; i++)
        {
            a[i]=read();
            //scanf("%d", &a[i]);
            b[i] = a[i];
        }
        sort(b + 1, b + 1 + n);
        for (int i = 1; i <= n; i++)
            a[i] = lower_bound(b + 1, b + 1 + n, a[i]) - b;
    //    cout << -1 << endl;
        for (int i = 1; i <= n; i++)
        {
           // cout << a[i] << endl;
            dp[i][1] = 1;
            for (int j = 2; j <= i; j++)
                dp[i][j] = Sum(j - 1, a[i] - 1);
            for (int j = 1; j <= i; j++)
                Update(j, a[i], dp[i][j]);
        }
        long long ans = 0;
        for (int i = 1; i <= n; i++)
            {
                ans = (ans + dp[i][m]);
                while(ans>=mod)
                    ans-=mod;// % mod;
                //cout << dp[i][m] << endl;
            }
        printf("Case #%d: %lld\n", ++cas, ans);
    }
    return 0;
}
时间: 2024-12-15 01:54:29

2015南阳CCPC C - The Battle of Chibi DP的相关文章

2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, two generals whose names are Cao Cao and Shao Yuan are fighting in Guandu. The battle of Guandu was a great battle and the two armies were fighting at

2015南阳CCPC D - Pick The Sticks 背包DP.

D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b

2015南阳CCPC A - Secrete Master Plan 水题

D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a pocket. The plan instructs how to deploy soldiers on the four corners of the city wall. Unfortunately, w

2015南阳CCPC H - Sudoku 暴力

H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. F

2015南阳CCPC H - Sudoku 数独

H - Sudoku Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. First of all, he tried to generate a 4×4 board

2015南阳CCPC E - Ba Gua Zhen 高斯消元 xor最大

Ba Gua Zhen Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description During the Three-Kingdom period, there was a general named Xun Lu who belonged to Kingdom Wu. Once his troop were chasing Bei Liu, he was stuck in the Ba Gua Zhen from Liang Zhuge.

2015南阳CCPC D - Pick The Sticks dp

D - Pick The Sticks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao

ccpc_南阳 C The Battle of chibi dp + 树状数组

题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma(dp[i][m]); 显然有:dp[i][j] = sigma(dp[k][j - 1]); 其中 1 <= k < i 且 a[k] < a[i]; 题目要求严格递增,这个限制怎么解决? hdu4719这道题同样是这样处理,即我们只需要从小到大dp就行了. 但是复杂度是O(n^3)的,显然