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

.

Input

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.

Output

Print the number of lucky ticket numbers, taken modulo 998244353

.

Examples
Input

Copy
4 2
1 8
Output

Copy
6
Input

Copy
20 1
6
Output

Copy
1
Input

Copy
10 5
6 1 4 0 3
Output

Copy
569725
Input

Copy
1000 7
5 4 0 1 8 3 2
Output

Copy
460571165
Note

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)的更多相关文章

  1. CF1096G Lucky Tickets 快速幂套FFT

    \(\color{#0066ff}{ 题目描述 }\) 一个\(n\)位数,每位可以是给出的\(k\)个数码中的一个数,可以有前导\(0\),输出前\(n/2\)位之和与后\(n/2\)位之和相等的方 ...

  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 ...

  3. BZOJ 3992: [SDOI2015]序列统计 快速幂+NTT(离散对数下)

    3992: [SDOI2015]序列统计 Description 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S ...

  4. 2014 Super Training #10 G Nostop --矩阵快速幂

    原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173 一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什 ...

  5. BZOJ 3992 DP+NTT+快速幂

    思路: 普通的DP很好想吧 f[i][j]+=f[i-1][j*s[k]]  前i个数  mod m=j 的个数 m是质数  模数是质数  这就很有趣了 那么我们就求出来原根  所有的数都取指数 搞出 ...

  6. AtCoder AGC031D A Sequence of Permutations (群论、置换快速幂)

    题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_d 题解 这居然真的是个找规律神题... 首先要明白置换的一些基本定义,置换\(p\)和\(q ...

  7. Codeforces632E Thief in a Shop(NTT + 快速幂)

    题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...

  8. 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]\] 其中\ ...

  9. 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− ...

随机推荐

  1. Git工作区、暂存区和版本库

    基本概念 我们先来理解下Git 工作区.暂存区和版本库概念 工作区:就是你在电脑里能看到的目录. 暂存区:英文叫stage, 或index.一般存放在 ".git目录下" 下的in ...

  2. golang martini 源码阅读笔记之inject

    martini是go语言写的一个超级轻量的web开源框架,具体源码可在github搜索找到.13年那会开始接触go语言时有稍微看过这个框架,由于之后没有继续使用go就慢慢忽略了,最近由于手头项目可能会 ...

  3. Confluence 6 LDAP 成员结构设置

    用户组成员属性(Group Members Attribute) 这个属性字段将在载入用户组成员的时候使用.例如: member 用户成员属性(User Membership Attribute) 这 ...

  4. qbxt联赛集训d1t3

    题意 给出一个长度为n的序列,求所有区间的区间最小值乘区间最大值的和.(n<=1e5) solution:

  5. Nastya Is Buying Lunch CodeForces - 1136D (排列)

    大意: 给定n排列, m个pair, 每个pair(u,v), 若u,v相邻, 且u在v左侧, 则可以交换u和v, 求a[n]最多向左移动多少 经过观察可以发现, 尽量先用右侧的人与a[n]交换, 这 ...

  6. java MongoDB查询(一)简单查询

    前言 MongoDB的java驱动提供了查询的功能,查询条件也是bson对象,这篇就看下怎么进行简单的数据查询 1.数据结构 集合:firstCollection 数据内容: { "_id& ...

  7. Eclipse 汉化方法

    1 打开 http://www.eclipse.org/babel/downloads.php 2  复制 http://download.eclipse.org/technology/babel/u ...

  8. ASP.NET的内置对象 —— Request 对象

    Request 对象最大的用途在于提交表单信息. (可获取页面间传递的值.客户端的 IP 地址等) 3.2.2 获取页面间传送的值 获取页面传送参数值是 Request 对象最广泛的应用之一. ASP ...

  9. RabbitMQ脑裂问题解决方案调查

    现象: RabbitMQ GUI上显示 Network partition detectedMnesia reports that this RabbitMQ cluster has experien ...

  10. HashTable vs HashMap(三)

    HashTable的应用非常广泛,HashMap是新框架中用来代替HashTable的类,也就是说建议使用HashMap,不要使用HashTable. 可能你觉得HashTable很好用,为什么不用呢 ...