HDU 5464Clarke and problem(DP)

Clarke and problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 289    Accepted Submission(s): 131

Problem Description

Clarke is a patient with multiple personality disorder. One day, Clarke turned into a student and read a book.

Suddenly, a difficult problem appears:

You are given a sequence of number a1,a2,...,an and
a number p.
Count the number of the way to choose some of number(choose none of them is also a solution) from the sequence that sum of the numbers is a multiple of p(0 is
also count as a multiple of p).
Since the answer is very large, you only need to output the answer modulo 109+7

Input

The first line contains one integer T(1≤T≤10) -
the number of test cases.

T test
cases follow.

The first line contains two positive integers n,p(1≤n,p≤1000)

The second line contains n integers a1,a2,...an(|ai|≤109).

Output

For each testcase print a integer, the answer.

Sample Input

1
2 3
1 2

Sample Output

2

Hint:
2 choice: choose none and choose all.

简单DP题  求n个数中 m个数的和是p的倍数  m~(0-n)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

int dp[1010][1010];
int num[1010];
const int Mod=1e9+7;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,p;
        scanf("%d%d",&n,&p);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        int sum=1;
        for(int i=1;i<=n;i++)
        {
            num[i]%=p;
            if(num[i]<0) num[i]+=p;
            for(int j=0;j<p;j++) dp[i][j]=dp[i-1][j];
            dp[i][num[i]]=(dp[i][num[i]]+1)%Mod;
            for(int j=0;j<p;j++)
            {
                dp[i][(num[i]+j)%p]=(dp[i-1][j]+dp[i][(num[i]+j)%p])%Mod;
            }
        }
        printf("%d\n",dp[n][0]+1);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-13 06:40:11

HDU 5464Clarke and problem(DP)的相关文章

HDU 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

hdu 2296 aC自动机+dp(得到价值最大的字符串)

Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3180    Accepted Submission(s): 1033 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a rom

hdu 2457 AC自动机+dp

DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2004    Accepted Submission(s): 1085 Problem Description Biologists finally invent techniques of repairing DNA that contains segments c

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

HDU 4833 Best Financing DP

Best Financing Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 120    Accepted Submission(s): 24 Problem Description 小 A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在

HDU 4832 Chess (DP)

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24    Accepted Submission(s): 10 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘上的走法遵循十字路线.

HDU 2845 Beans (DP)

Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyo

HDU Tickets(简单的dp递推)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 972    Accepted Submission(s): 495 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,