zoukankan      html  css  js  c++  java
  • hdu 5464 Clarke and problem(dp)

    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(1T10) - the number of test cases. 
    T test cases follow. 
    The first line contains two positive integers n,p(1n,p1000) 
    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

      设dp[i][j]表示到第i个元素余数为j的个数。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 #define modelo 1000000007
     6 int a[1005];
     7 int dp[1005][1005];
     8 int main()
     9 {
    10     int n,t,q,i,j;
    11     scanf("%d",&t);
    12     while (t--)
    13     {
    14        scanf("%d%d",&n,&q);
    15        for (i=1;i<=n;i++)
    16        {
    17           scanf("%d",&a[i]);
    18           a[i]=(a[i]%q+q)%q;
    19        }
    20        memset(dp,0,sizeof(dp));
    21        dp[0][0]=1;
    22        for (i=1;i<=n;i++)
    23        {
    24           for (j=0;j<q;j++)
    25           dp[i][j]=(dp[i-1][j]+dp[i-1][(j+a[i])%q])%modelo;
    26        }
    27        printf("%d
    ",dp[n][0]);
    28     }
    29     return 0;
    30 }
  • 相关阅读:
    scp命令
    遇到的错误解决方法
    阿里云挂载数据盘
    正则表达式
    python例子三
    Linux shell快捷键
    《超级产品的本质:汽车大王亨利福特自传》书评
    学习嵌入式的一点建议【转】
    win7使用USB转串口连接mini2440方法
    吐血原创:mini2440和win7笔记本利用无路由功能的交换机共享上网(使用x-router软路由)
  • 原文地址:https://www.cnblogs.com/pblr/p/4825926.html
Copyright © 2011-2022 走看看