题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1125

题意:

给你n个数,q次询问,每次询问问你取其中m个数是d的整数倍的方案数。

题意:

dp[i][j][k] 表示前i个数, %d=j, 取了k个的方案数。

ID SUBMISSION TIME PROBLEM SOURCE CPU MEMORY VERDICT
839200 2016-10-15 14:59:00 1125 - Divisible Group Sums C++ 0.012 1688
Accepted
839186 2016-10-15 14:35:08 1125 - Divisible Group Sums C++ 0.336 1688
Accepted

没优化前 0.336:

 #include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = ;
LL dp[][][]; //dp[i][j][k]表示包含i
int a[N], b[N]; void solve(int n, int m, int d) {
memset(dp, , sizeof(dp));
for(int i = ; i <= n; ++i) {
dp[i][b[i]][] = ;
for(int j = ; j < i; ++j) {
for(int x = ; x < d; ++x) {
for(int y = ; y <= m; ++y) {
dp[i][(b[i] + x) % d][y] += dp[j][x][y - ];
}
}
}
}
} int main()
{
int t, n, q;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d %d", &n, &q);
for(int i = ; i <= n; ++i) {
scanf("%d", a + i);
}
printf("Case %d:\n", ca);
while(q--) {
int d, m;
scanf("%d %d", &d, &m);
for(int i = ; i <= n; ++i) {
b[i] = (a[i] % d + d) % d;
}
solve(n, m, d);
LL ans = ;
for(int i = ; i <= n; ++i) {
ans += dp[i][][m];
}
printf("%lld\n", ans);
}
}
return ;
}

优化后 0.012:

 #include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = ;
LL dp[][][]; //dp[i][j][k]表示前i个
int a[N], b[N]; void solve(int n, int m, int d) {
memset(dp, , sizeof(dp));
for(int i = ; i <= n; ++i) {
dp[i][b[i]][] = ;
for(int j = ; j < d; ++j) {
for(int x = ; x <= m; ++x) {
dp[i][(b[i] + j) % d][x] += dp[i - ][j][x - ];
dp[i][j][x] += dp[i - ][j][x];
}
}
}
} int main()
{
int t, n, q;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d %d", &n, &q);
for(int i = ; i <= n; ++i) {
scanf("%d", a + i);
}
printf("Case %d:\n", ca);
while(q--) {
int d, m;
scanf("%d %d", &d, &m);
for(int i = ; i <= n; ++i) {
b[i] = (a[i] % d + d) % d;
}
solve(n, m, d);
printf("%lld\n", dp[n][][m]);
}
}
return ;
}

Light oj 1125 - Divisible Group Sums (dp)的更多相关文章

  1. lightoj 1125 - Divisible Group Sums (dp)

    Given a list of N numbers you will be allowed to choose any M of them. So you can choose in NCM ways ...

  2. LightOJ1125 Divisible Group Sums

    Divisible Group Sums Given a list of N numbers you will be allowed to choose any M of them. So you c ...

  3. Divisible Group Sums

    Divisible Group Sums Given a list of N numbers you will be allowed to choose any M of them. So you c ...

  4. LightOJ1125 Divisible Group Sums(DP)

    题目问从N个数中取出M个数,有多少种取法使它们的和能被D整除. dp[i][j][k]表示,前i个数取出j个数模D的余数为k的方案数 我用“我为人人”的方式来转移,就从i到i+1转移,对于第i+1个数 ...

  5. Light OJ 1031 - Easy Game(区间dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅 ...

  6. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

  7. light oj 1422 Halloween Costumes (区间dp)

    题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...

  8. Light OJ 1364 Expected Cards (期望dp,好题)

    题目自己看吧,不想赘述. 参考链接:http://www.cnblogs.com/jianglangcaijin/archive/2013/01/02/2842389.html #include &l ...

  9. light oj 1205 - Palindromic Numbers 数位DP

    思路:搜索的时候是从高位到低位,所以一旦遇到非0数字,也就确定了数的长度,这样就知道回文串的中心点. 代码如下: #include<iostream> #include<cstdio ...

随机推荐

  1. Android stadio

    Android stadio 最近遇到大问题,就是主功能行.但是让它做库工程,他就不管用. 但是在eclipse里面就可以.

  2. springboot学习资料汇总

    收集Spring Boot相关的学习资料,Spring Cloud点这里 推荐博客 纯洁的微笑 程序猿DD liaokailin的专栏 Spring Boot 揭秘与实战 系列 catoop的专栏 简 ...

  3. Helloworld 在jvm 内存图

    HelloWorld.java源码如下:   public class HelloWorld { public static void main(String[] args) { String s ; ...

  4. 聊聊、Java 命令 第一篇

    网上很多讲 Javac 和 Java 命令的,我觉得还是要自己写一写,做一个自己的总结,也方便以后查询. 开始之前先看看 help 命令,基本上任何一个软件都会提供这个命令. 没有什么比 -help ...

  5. jquery使用ajax传内容到asp.net乱码解决【转】

    转自:http://www.cnblogs.com/qiantuwuliang/archive/2009/08/02/1537160.html#undefined Jquery强大的功能越来越收到广大 ...

  6. IO Streams:来源于命令行的IO

    简介 程序经常从命令行运行并与在命令行环境中的用户交互.Java平台支持这种互动的方式有两种:通过标准流,通过控制台 标准流 标准流是许多操作系统的一项功能.默认情况下,他们从键盘输入读取和输出到显示 ...

  7. jQuery ajax使用$(this).parent()无效解决方法

    div=$(this).parent(); //先获取父级元素 div.remove(); //再删除 $(".delStu").click(function () {       ...

  8. Ubuntu系统设置中心不见了

    sudo apt-get install unity-control-center 执行安装上述即可 原因是因为在安装搜狗输入法的时候卸载了ibuts与系统控制中心

  9. C#,一种简单的方式实现滚动鼠标缩放图片,平移

    1.缩放 private void ImageShow_Load(object sender, EventArgs e) { pictureBox1.Load(@"E:\SQ1.jpg&qu ...

  10. [SDOI2009][bzoj1878] HH的项链 [莫队模板题]

    题面: 传送门 思路: 就是一道莫队的模板题目...... 开一个1000000的数组记录每个数出现的次数,然后每次从1到0或者从0到1更新答案 莫队讲解看这里:莫队 Code: #include&l ...