Light oj 1125 - Divisible Group Sums (dp)
题目链接: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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- LightOJ1125 Divisible Group Sums(DP)
题目问从N个数中取出M个数,有多少种取法使它们的和能被D整除. dp[i][j][k]表示,前i个数取出j个数模D的余数为k的方案数 我用“我为人人”的方式来转移,就从i到i+1转移,对于第i+1个数 ...
- Light OJ 1031 - Easy Game(区间dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅 ...
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
- light oj 1422 Halloween Costumes (区间dp)
题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...
- Light OJ 1364 Expected Cards (期望dp,好题)
题目自己看吧,不想赘述. 参考链接:http://www.cnblogs.com/jianglangcaijin/archive/2013/01/02/2842389.html #include &l ...
- light oj 1205 - Palindromic Numbers 数位DP
思路:搜索的时候是从高位到低位,所以一旦遇到非0数字,也就确定了数的长度,这样就知道回文串的中心点. 代码如下: #include<iostream> #include<cstdio ...
随机推荐
- char* 与char[]区别
[转] 最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区(认为无区别),导致有时出现“难以理解”的错误.一时也不能说得很明白,网上也 ...
- Java文件 ---文件相关操作
创建文件 file.createNewFile() 注:若该文件对象未指定文件路径,则文件创建于相对路径中,即工程目录下.(“../”表示上级文件目录,相对路径前面不加“/”,eg:bin/text. ...
- IOS开发---菜鸟学习之路--(十六)-将Image转换为Base64
我们直接在.m文件的引用头文件部分 和 @interface AddPictureViewController () 之间 加入 增加部分的代码 然后就可以使用图片转Base64了 #impor ...
- linux环境搭建系列之Apache MQ安装
1.创建文件夹 #mkdir MQ 2.解压 #tar -vxf apache-activemq-5.14.3-bin.tar.gz 3.进入解压后的目录 # cd apache-activemq-5 ...
- php伪随机数漏洞 以及脚本php_mt_seed的使用教程
前几天在群里看到了一个题目,发现自己没有接触过这个伪随机数这个漏洞,在此记录下. 搜索这两个函数 mt_scrand() mt_rand() mt_scrand(seed)这个函数的意思,是通过分发s ...
- 最简单的RSA及其几个网站和工具
最简单的形式 给你公钥和一个密文. flag.enc就是密文,我们用记事本是看不出什么的,其实也不用看,因为后边的解密是直接用脚本读取文件的,只需要知道这是密文. pub.pem就是公钥,用记事本打开 ...
- MCMC 浅谈
# MCMC 浅谈 1. 采样(sampling)是什么 MCMC在采样算法中有着举足轻重的地位,那么什么是采样?采样就是根据某种分布生成样本.举个例子,线性同余发生器就是根据均匀分布生成样本,这就很 ...
- Linux内核使用毫秒延时函数
毫秒延时函数:mdelay() 微妙延时函数:ndelay() #ifndef mdelay #define mdelay(n) (/ (__builtin_constant_p(n) &&a ...
- 浅谈JavaScript中的函数问题
前面的话:JavaScript可运行在所有主要平台的主流浏览器上,也可运行在每一个主流操作系统的服务器端上.所以呢,要想成为一名优秀的全栈工程师,必须懂得JavaScript语言.这是我整理的JS的部 ...
- Ubuntu下建立Pycharm快捷方式
修改 Exec, Icon 路径,将文件保存 pycharm.desktop,拖到unity侧边栏 [Desktop Entry]Categories=Development;Comment[zh_C ...