[CF632E]Thief in a Shop
题目大意:有一个小偷,拿$k$个东西,有$n$种产品,每种产品都有无限多个。对于每个第$i$ 种产品,它的价值是$A_i$。可能偷走的物品价值之和。
题解:对于所有的物品构造生成函数$F(x)=\sum\limits_{i\in A}x^i$,取$k$个物品相当于取其中的$k$项相乘,输出$F^k(x)$中不为零的项就行了。(这道题模数$998244353$和$1004535809$都被$hack$了,看$Weng\_weijie\;dalao$的题解得双模数没被卡,于是就$A$了)(这道题似乎可以用$DP$,但我不怎么会)
卡点:无
C++ Code:
#include <cstdio>
#include <algorithm>
#define maxn 1 << 20 | 3
const int G = 3;
int mod, ans;
int lim, ilim, s, rev[maxn], Wn[maxn];
inline int pw(int base, long long p) {
base %= mod, p %= mod - 1;
int ans = 1;
for (; p; p >>= 1, base = 1ll * base * base % mod) if (p & 1) ans = 1ll * ans * base % mod;
return ans;
}
inline int Inv(int x) {
return pw(x, mod - 2);
}
inline void up(int &a, int b) {if ((a += b) >= mod) a -= mod;}
inline void NTT(int *A, int op) {
for (int i = 0; i < lim; i++) if (i < rev[i]) std::swap(A[i], A[rev[i]]);
for (int mid = 1; mid < lim; mid <<= 1) {
int t = lim / mid >> 1;
for (int i = 0; i < lim; i += mid << 1) {
for (int j = 0; j < mid; j++) {
int W = op ? Wn[t * j] : Wn[lim - t * j];
int X = A[i + j], Y = 1ll * A[i + j + mid] * W % mod;
up(A[i + j], Y), up(A[i + j + mid] = X, mod - Y);
}
}
}
if (!op) for (int i = 0; i < lim; i++) A[i] = 1ll * A[i] * ilim % mod;
}
inline void init(int n, int mod) {
::mod = mod;
lim = 1, s = -1; while (lim < n) lim <<= 1, s++; ilim = Inv(lim);
for (int i = 0; i < lim; i++) rev[i] = rev[i >> 1] >> 1 | (i & 1) << s;
int W = pw(G, (mod - 1) / lim);
Wn[0] = 1; for (int i = 1; i <= lim; i++) Wn[i] = 1ll * Wn[i - 1] * W % mod;
}
int n, k;
int a[maxn], b[maxn];
int main() {
scanf("%d%d", &n, &k);
for (int i = 0, tmp; i < n; i++) scanf("%d", &tmp), a[tmp] = b[tmp] = 1;
init(1 << 20, 998244353);
NTT(a, 1);
for (int i = 0; i < lim; i++) a[i] = pw(a[i], k);
NTT(a, 0);
init(1 << 20, 1004535809);
NTT(b, 1);
for (int i = 0; i < lim; i++) b[i] = pw(b[i], k);
NTT(b, 0);
for (int i = 0; i < lim; i++) if (a[i] | b[i]) printf("%d ", i);
return 0;
}
[CF632E]Thief in a Shop的更多相关文章
- CF632E Thief in a Shop 和 CF958F3 Lightsabers (hard)
Thief in a Shop n个物品每个价值ai,要求选k个,可以重复.问能取到哪几个价值? 1 ≤ n, k ≤ 1000,1 ≤ ai ≤ 1000 题解 将选一个物品能取到的价值的01生成函 ...
- codeforces 632+ E. Thief in a Shop
E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input standard ...
- codeforces 632E. Thief in a Shop fft
题目链接 E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input stan ...
- C - Thief in a Shop - dp完全背包-FFT生成函数
C - Thief in a Shop 思路 :严格的控制好k的这个数量,这就是个裸完全背包问题.(复杂度最极端会到1e9) 他们随意原来随意组合的方案,与他们都减去 最小的 一个 a[ i ] 组合 ...
- codeforces Educational Codeforces Round 9 E - Thief in a Shop
E - Thief in a Shop 题目大意:给你n ( n <= 1000)个物品每个物品的价值为ai (ai <= 1000),你只能恰好取k个物品,问你能组成哪些价值. 思路:我 ...
- Educational Codeforces Round 9 E. Thief in a Shop dp fft
E. Thief in a Shop 题目连接: http://www.codeforces.com/contest/632/problem/E Description A thief made hi ...
- Educational Codeforces Round 9 E. Thief in a Shop NTT
E. Thief in a Shop A thief made his way to a shop. As usual he has his lucky knapsack with him. Th ...
- CF632E: Thief in a Shop(快速幂+NTT)(存疑)
A thief made his way to a shop. As usual he has his lucky knapsack with him. The knapsack can contai ...
- Codeforces632E Thief in a Shop(NTT + 快速幂)
题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...
随机推荐
- java基础必备单词讲解 day six
development development development development 开发 development developmentenvironment environment en ...
- PHP:(一)安装并使用PHP
php的安装分为两个部分:环境安装配置和开发工具 一.集成环境安装配置 (一)安装 选择:wampserver或者xampp 我采用的是xampp. 在http://www.sourceforce.n ...
- Tomcat的部署+第一个Servlet
Tomcat部署 1.下载tomcat,添加到eclipse 2.配置环境变量(path) 3.win+r,输入Startup(如果没用,就管理员启动命令) 或者找到tomcat安装包,在bin目录找 ...
- SQLSERVER存储过程基本语法使用
一.定义变量 --简单赋值 declare @a int print @a --使用select语句赋值 ) select @user1='张三' print @user1 ) print @user ...
- Docker自学纪实(二)Docker基本操作
安装docker 以CentOS7为例: 安装:yum -y install docker 启动:systemctl start docker 设置开机自启:systemctl enable dock ...
- AngularJS常见面试题
本文引自:https://segmentfault.com/a/1190000005836443 问题来源:如何衡量一个人的 AngularJS 水平? ng-if 跟 ng-show/hide 的区 ...
- linux 特殊命令(一)
1.ifconfig 网卡配置:ifconfig [网络设备] [参数] 1) up 启动指定网络设备/网卡. 2) down 关闭指定网络设备/网卡.该参数可以有效地阻止通过指定接口的IP信息流, ...
- 交叉编译qt5.6
按照网上的攻略编译QT5.6 https://www.lijingquan.net/2016/07/08/build-kernel-busybox-qt5-6-tslib-imx28/ 出现问题,找不 ...
- Flask错误收集 【转】
感谢大佬 ---> 原文链接 一.pydev debugger: process XXXXX is connecting 这个错误网上找了很多资料都无法解决,尝试过多种方法后,对我来说,下面这个 ...
- Spring---BeanFactory与ApplicationContext简介
BeanFactory概念 Spring通过一个配置文件描述bean和bean之间的依赖关系,然后利用java语言的反射功能实例化bean,并建立bean之间的依赖关系.Spring的IOC容器在完成 ...