CF1096. G. Lucky Tickets(快速幂NTT)
All bus tickets in Berland have their numbers. A number consists of n digits (n is even). Only k decimal digits d1,d2,…,dk can be used to form ticket numbers. If 0 is among these digits, then numbers may have leading zeroes. For example, if n=4 and only digits 0 and 4 can be used, then 0000, 4004, 4440 are valid ticket numbers, and 0002, 00, 44443
are not.
A ticket is lucky if the sum of first n/2
digits is equal to the sum of remaining n/2
digits.
Calculate the number of different lucky tickets in Berland. Since the answer may be big, print it modulo 998244353
.
The first line contains two integers n
and k (2≤n≤2⋅105,1≤k≤10) — the number of digits in each ticket number, and the number of different decimal digits that may be used. n
is even.
The second line contains a sequence of pairwise distinct integers d1,d2,…,dk
(0≤di≤9)
— the digits that may be used in ticket numbers. The digits are given in arbitrary order.
Print the number of lucky ticket numbers, taken modulo 998244353
.
4 2
1 8
6
20 1
6
1
10 5
6 1 4 0 3
569725
1000 7
5 4 0 1 8 3 2
460571165
In the first example there are 6
lucky ticket numbers: 1111, 1818, 1881, 8118, 8181 and 8888
.
There is only one ticket number in the second example, it consists of 20
digits 6. This ticket number is lucky, so the answer is 1.
题意:给定你K种数可以选,问你有多少排列,使得前半部分的和等于后半部分的和。
思路:我们枚举“和”的大小,令A[i]表示长度为N/2时和为i的方案数,那么答案就是所有的A[i]*A[i];
怎么就Ai呢,就是一个多项式(P0.X0+P1*X1+...*P9*X9)^(N/2);p是0或者1;
多项式的N次,我们可以用快速幂+FFT来加速,这里要取模,写的NTT。然后就是可以直接一次DFT+快速幂+IDFT。
(之前纠结过为什么可以一次DFT就搞定了,而不是logN次,现在看来是对的,正确性我还需要学习一下。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define ll long long
const int G=;
const int maxn=;
const int Mod=;
int mod,n,k,rev[maxn],lim,ilim,s,wn[maxn+];
std::vector<int> v;
inline int pow(int x, int y) {
int ans=;
for(;y;y>>=,x=(ll)x*x%mod)
if(y&) ans=(ll)ans*x%mod;
return ans;
}
inline int& up(int& x, int y) { if ((x+=y)>=mod) x-=mod; return x; }
inline void NTT(int* A, int typ) {
rep(i,,lim-) if (i<rev[i]) swap(A[i], A[rev[i]]);
for (int i=;i<lim;i+=i) {
const int t=lim/i/;
for (int j=;j<lim;j+=i+i) {
for (int k=;k<i; k++) {
int w=typ?wn[t*k]:wn[lim-t*k];
int x=A[k+j],y=(ll)w*A[k+j+i]%mod;
up(A[k+j],y),up(A[k+j+i]=x,mod-y);
}
}
}
if (!typ) rep(i,,lim-) A[i]=(ll)ilim*A[i]%mod;
}
inline void init(int len,int tmod) {
mod=tmod; lim=; s=-;
while(lim<len) lim+=lim,s++; ilim=pow(lim,mod-);
rep(i,,lim-) rev[i]=rev[i>>]>>|(i&)<<s;
int w=pow(G,(mod-)/len);
wn[]=;
rep(i,,lim) wn[i]=(ll)(wn[i-])*w%mod;
}
int A[maxn];
int main() {
scanf("%d%d",&k,&n); k/=;
int x,ans=; rep(i,,n) scanf("%d",&x), A[x]=B[x]=;
init(, );
NTT(A, );
rep(i,,lim-) A[i]=pow(A[i],k);
NTT(A, );
rep(i,,) (ans+=(ll)A[i]*A[i]%Mod)%=Mod;
printf("%d\n",ans);
return ;
}
CF1096. G. Lucky Tickets(快速幂NTT)的更多相关文章
- CF1096G Lucky Tickets 快速幂套FFT
\(\color{#0066ff}{ 题目描述 }\) 一个\(n\)位数,每位可以是给出的\(k\)个数码中的一个数,可以有前导\(0\),输出前\(n/2\)位之和与后\(n/2\)位之和相等的方 ...
- 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 ...
- BZOJ 3992: [SDOI2015]序列统计 快速幂+NTT(离散对数下)
3992: [SDOI2015]序列统计 Description 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S ...
- 2014 Super Training #10 G Nostop --矩阵快速幂
原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...
- BZOJ 3992 DP+NTT+快速幂
思路: 普通的DP很好想吧 f[i][j]+=f[i-1][j*s[k]] 前i个数 mod m=j 的个数 m是质数 模数是质数 这就很有趣了 那么我们就求出来原根 所有的数都取指数 搞出 ...
- AtCoder AGC031D A Sequence of Permutations (群论、置换快速幂)
题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_d 题解 这居然真的是个找规律神题... 首先要明白置换的一些基本定义,置换\(p\)和\(q ...
- Codeforces632E Thief in a Shop(NTT + 快速幂)
题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...
- Codeforces1096G Lucky Tickets(NTT优化dp)
设\(f[i][j]\)表示填了\(i\)个数,数位和为\(j\)的方案数 于是方程为: \[f[i][j]=\sum_{k=0}^9 f[i-1][j-k]*[CanUse[k]==1]\] 其中\ ...
- 2018.12.31 bzoj3992: [SDOI2015]序列统计(生成函数+ntt+快速幂)
传送门 生成函数简单题. 题意:给出一个集合A={a1,a2,...as}A=\{a_1,a_2,...a_s\}A={a1,a2,...as},所有数都在[0,m−1][0,m-1][0,m− ...
随机推荐
- Redis之字符串类型命令
String(字符串) string 是 redis 最基本的类型,你可以理解成与 Memcached 一模一样的类型,一个 key 对应一个 value. string 类型是二进制安全的.意思是 ...
- Linux-Load Average解析
load Average 转自:http://www.blogjava.net/sliverfancy/archive/2013/04/17/397947.html 1.1:什么是Load?什么是Lo ...
- JDBC 与 Bean Shell的使用(一)获取值,并且传递
1.在使用Jmeter进行接口测试的时候,会使用到JDBC,连接数据库,操作数据库其得到的数据后续操作需要使用,这里我们使用了BeanShell的概念来获取JDBC的返回值 如下说明了联合使用的2种方 ...
- java克隆机制
看了下面博客就很明白了 http://www.cnblogs.com/Qian123/p/5710533.html#_label0 java对象创建方式有三种: 1.通过new对象 2.通过java克 ...
- 正睿 2019 省选附加赛 Day1 T1 考考试
比较奇怪的一个枚举题. 注意到10=2*5,所以10^k的二进制表示一定恰好在末尾有k个0. 考虑从小到大去填这个十进制数. 填的时候记录一下当前的二进制表示. 每次尝试去填0或者10^k. 如果要填 ...
- 『Pickle』数据结构持久化模块_常用方法记录
可以把数据结构保存进文件并直接读出, 不论读取或者是保存,我们都需要借助open()函数,并且是二进制方式(‘wb’,‘rb’) json模块可以把字典结构改写为string然后保存,并可以反向读取字 ...
- poj2417 Discrete Logging BSGS裸题
给a^x == b (mod c)求满足的最小正整数x, 用BSGS求,令m=ceil(sqrt(m)),x=im-j,那么a^(im)=ba^j%p;, 我们先枚举j求出所有的ba^j%p,1< ...
- json.dumps loads 终于区分出来了
import json dict= {1:2, 3:4, "} print type(dict), dict # test json.dumps json_str = json.dumps( ...
- Java使用POI读取和写入Excel指南
Java使用POI读取和写入Excel指南 做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃 ...
- Coconuts, Revisited(递推+枚举+模拟)
Description The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening ...