hdu 5464 Clarke and problem

题意:有n个数,a_0, a_1, ... a_(n-1),从中选取一部分数(可以一个都不选),使得其和为p的倍数,问方案数。

解:这题怎么说呢。。。就差在把背包这俩字甩我脸上了。。。but,昨天还是没有做出来。-_-!

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;

typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long uLL;

const int N = 1005;
const double PI = acos(-1.0);
const double eps = 1e-6;
const double INF = 1e9+7;
const int MOD = 1000000007;

int f[N][N];
int a[N];
int n, p;

int main() {
    //freopen("data.in", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &p);
        memset(f, 0, sizeof(f));
        //int off = N;
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            a[i] = (a[i]%p + p)%p;
        }
        f[0][0] = 1;
        for(int i = 1; i <= n; ++i) {
            for(int j = 0; j <= p; ++j) {
                    f[i][j] = (f[i][j] + f[i-1][j])%MOD;
                //if(f[i-1][j + off] != 0) {
                    f[i][(j + a[i])%p] = (f[i][(j + a[i])%p] % MOD + f[i-1][j] % MOD) % MOD;
                //}
            }
        }
        printf("%d\n", f[n][0] % MOD);
    }
    return 0;
}
时间: 2024-10-11 07:18:33

hdu 5464 Clarke and problem的相关文章

HDU 5464 Clarke and problem 动态规划

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5464 Clarke and problem Accepts: 130 Submissions: 781 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 克拉克是一名人格分裂患者.某一天,克拉克分裂成了一个学生,在做题. 突然一道难题难到了克拉克,这道题是这样的: 给你nn个数

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

HDU 5464 ( Clarke and problem ) (dp)

dp[i][j] := 前i个数和为j的情况(mod p) dp[i][j] 分两种情况 1.不选取第i个数 -> dp[i][j] = dp[i-1][j] 2.   选取第i个数 -> dp[i][j] = dp[i-1][t] ((t+a[i])%p==j) (为什么很简单的题,思路也有了,比赛的时候就是写不对呢?) #include <iostream> #include <cstdio> #include <cstring> using names

BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle

今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<queue> #include<vector> #include<stack> #include<vector> #include<map>

hdu 5293 Tree chain problem(树链剖分+树形dp)

题目链接:hdu 5293 Tree chain problem 维护dp[u], sum[u],dp[u]表示以u为根节点的子树的最优值.sum[u]表示以u节点的所有子节点的dp[v]之和.对于边a,b,w,在LCA(a,b)节点的时候进行考虑.dp[u] = min{dp[u], Sum(a,b) - Dp(a,b) + sum[u] | (ab链上的点,不包括u } #pragma comment(linker, "/STACK:1024000000,1024000000")

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as

hdu 2576 Another Sum Problem

题目大意:求前n项和的前n项和. 数学推导题,f(n)=n*(n+1)*(n+2)/6 推导思路如下: #include"cstdio" #include"cstring" #include"cmath" #include"cstdlib" #include"iostream" #include"algorithm" #include"queue" using nam