Codeforces 1096G. Lucky Tickets【生成函数】
题目大意
很简单自己看
思路
考虑生成函数(为啥tags里面有一个dp啊)
显然,每一个指数上是否有系数是由数集中是否有这个数决定的
有的话就是1没有就是0
然后求出这个生成函数的\(\frac{n}{2}\)次方
把每一项的系数全部平方加起来。。没了
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> Poly;
const int N = 3e6 + 10;
const int Mod = 998244353;
const int G = 3;
int add(int a, int b, int mod = Mod) {
return (a += b) >= mod ? a - mod : a;
}
int sub(int a, int b, int mod = Mod) {
return (a -= b) < 0 ? a + mod : a;
}
int mul(int a, int b, int mod = Mod) {
return 1ll * a * b % mod;
}
int fast_pow(int a, int b, int mod = Mod) {
int res = 1;
for (; b; b >>= 1, a = mul(a, a, mod))
if (b & 1) res = mul(res, a, mod);
return res;
}
int w[N][2];
void init() {
for (int i = 1; i < (1 << 21); i <<= 1) {
w[i][0] = w[i][1] = 1;
int wn = fast_pow(G, (Mod - 1) / (i << 1));
for (int j = 1; j < i; j++)
w[i + j][0] = mul(w[i + j - 1][0], wn);
wn = fast_pow(G, Mod - 1 - (Mod - 1) / (i << 1));
for (int j = 1; j < i; j++)
w[i + j][1] = mul(w[i + j - 1][1], wn);
}
}
void transform(int *t, int len, int typ) {
for (int i = 0, j = 0, k; j < len; j++) {
if (i > j) swap(t[i], t[j]);
for (k = (len >> 1); k & i; k >>= 1) i ^= k;
i ^= k;
}
for (int i = 1; i < len; i <<= 1) {
for (int j = 0; j < len; j += i << 1) {
for (int k = 0; k < i; k++) {
int x = t[j + k], y = mul(t[j + k + i], w[i + k][typ]);
t[j + k] = add(x, y);
t[j + k + i] = sub(x, y);
}
}
}
if (typ) return;
int invlen = fast_pow(len, Mod - 2);
for (int i = 0; i < len; i++)
t[i] = mul(t[i], invlen);
}
Poly fast_pow(Poly a, int b) {
int len = 1 << (int) ceil(log2(a.size()));
a.resize(len);
transform(&a[0], len, 1);
for (int i = 0; i < len; i++)
a[i] = fast_pow(a[i], b);
transform(&a[0], len, 0);
return a;
}
int n, k;
int main() {
init();
scanf("%d %d", &n, &k);
Poly a((int) 2e6);
for (int i = 1; i <= k; i++) {
int x;
scanf("%d", &x);
a[x] = 1;
}
a = fast_pow(a, n / 2);
int ans = 0;
for (int i = 0; i < (signed) a.size(); i++)
ans = add(ans, mul(a[i], a[i]));
printf("%d", ans);
return 0;
}
Codeforces 1096G. Lucky Tickets【生成函数】的更多相关文章
- 2019.01.26 codeforces 1096G. Lucky Tickets(生成函数)
传送门 题意简述:现在有一些号码由000~999中的某些数字组成(会给出),号码总长度为nnn,问有多少个号码满足前n2\frac n22n个数码的和等于后n2\frac n22n个数码的和(保证 ...
- Codeforces - 1096G - Lucky Tickets - NTT
https://codeforc.es/contest/1096/problem/G 把数组分成前后两半,那么前半部分的各个值的表示方案的平方的和就是答案. 这些数组好像可以dp出来. 一开始设dp[ ...
- @codeforces - 1096G@ Lucky Tickets
目录 @description@ @solution@ @accepted code@ @details@ @description@ 已知一个数(允许前导零)有 n 位(n 为偶数),并知道组成这个 ...
- Codeforces Gym 100418J Lucky tickets 数位DP
Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- POJ-2346 Lucky tickets(线性DP)
Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3298 Accepted: 2174 Descrip ...
- CF1096. G. Lucky Tickets(快速幂NTT)
All bus tickets in Berland have their numbers. A number consists of n digits (n is even). Only k dec ...
- DP+高精度 URAL 1036 Lucky Tickets
题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...
- Ural 1036 Lucky Tickets
Lucky Tickets Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ...
- POJ 2346:Lucky tickets
Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3247 Accepted: 2136 Des ...
随机推荐
- Python mysql-SQL概要
2017-09-05 20:10:58 一.SQL语句及其种类 SQL使用关键字,表名,列名等组合成一条语句来描述操作的内容.关键字是指那些含义或者使用方法是先已经定义好的英语单词.根据RDBMS赋予 ...
- socket+django
1.socket 网络上任意两个程序之间要进行通信,需要依靠socket(端口).socket封装了TCP/IP协议,让网络通信基于TCP/IP协议的形式实现. socket可以翻译为插座,那么一个服 ...
- English trip -- Phonics 3 元音字母e
xu言: 额...今天给我上自然拼读的maple老师 - . -和上次给我上第二集自然拼读的是同一个老师.突然考了考我上次学的内容~感觉大脑一片空白.看来review不能光说而不下苦功夫啊... 元音 ...
- 最短路-Prim算法 dijkstra算法
HDU-1233 #include <iostream> #define INF 1000000 using namespace std; ][]; ]; ]; ]; ]; int mai ...
- IDEA设置类、方法注释模板
类注释模板 File -> Other Setting -> Default Setting打开默认设置 Editor -> File and Code Templates -> ...
- [NOI2018]你的名字(68pts)
SAM真让人头秃. 题面 https://www.luogu.org/problemnew/show/P4770 首先考虑 l=1,r=∣S∣的做法 如果对于ION2018的子串不用判重的话,对ION ...
- 最小生成树(模板 prim)
Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表, 表中列出了任意两城镇间修建道路 ...
- JS获取昨天/今天/今年第一天的方法
talk is cheap,show me the code! //获取昨天 var day1 = new Date(); day1.setTime(day1.getTime()-24*60*60*1 ...
- python之numpy的基本使用
https://blog.csdn.net/cxmscb/article/details/54583415 一.numpy概述 numpy(Numerical Python)提供了python对多维数 ...
- oracle 产生一个任意大小的随机数
SELECT DBMS_RANDOM.RANDOM FROM DUAL; 产生一个任意大小的随机数 SELECT ABS(MOD(DBMS_RANDOM.RANDOM,100)) F ...