codeforces1096G Lucky Tickets
题目链接:https://codeforces.com/problemset/problem/1096/G
大意:给出\(k\)个数码\(d_1,d_2,\cdots,d_k\),构造一个由这\(k\)个数码组成的\(n\)位数(可重复使用数码),使得该数的前\(\frac{n}{2}\)位数码之和等于后\(\frac{n}{2}\)位数码之和,求方案数
分析:转化一下题意就是说构造\(\frac{n}{2}\)位数,求构成数的各位数字之和的方案数,最后乘法原理乘一下即可
如果满足\(n\leq1000\)的话跑完全背包即可,然而数据放到了\(2·10^5\)
我们考虑一下如下的生成函数(其实不能称作标准的生成函数):\((x^{d_1}+x^{d_2}+\cdots+x^{d_k})^{\frac{n}{2}}\)
我们将它展开,每一项的次数就是表示出来的数的各位上的数码之和,系数就表示方案数(不就是完全背包么)
由于项数最大也就是\(2·10^6\),直接NTT+快速幂即可
#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define rep(i,a,b) for (i=a;i<=b;i++)
typedef long long ll;
#define maxd 998244353
#define pi acos(-1.0)
#define N 2000000
#define int long long
ll a[5004000],b[5000400];
int n,r[5004000],k,lim=1,cnt=0;
int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
int qpow(int x,int y)
{
int ans=1,sum=x;
while (y)
{
int tmp=y%2;y/=2;
if (tmp) ans=(1ll*ans*sum)%maxd;
sum=(1ll*sum*sum)%maxd;
}
return ans;
}
void ntt(int lim,ll *a,int typ)
{
int i;
for (i=0;i<lim;i++)
if (i<r[i]) swap(a[i],a[r[i]]);
int mid;
for (mid=1;mid<lim;mid<<=1)
{
int gn=qpow(3,(maxd-1)/(mid<<1));
int sta,len=mid<<1,j;
for (sta=0;sta<lim;sta+=len)
{
int g=1;
for (j=0;j<mid;j++,g=(g*gn)%maxd)
{
int x1=a[j+sta],y1=(g*a[j+sta+mid])%maxd;
a[j+sta]=(x1+y1)%maxd;
a[j+sta+mid]=(x1-y1+maxd)%maxd;
}
}
}
if (typ==-1) reverse(&a[1],&a[lim]);
}
void init()
{
n=read();k=read();
//memset(a,0,sizeof(a));
int i;n/=2;
for (i=1;i<=k;i++)
{int x=read();a[x]=1;}
while (lim<=N) {lim<<=1;cnt++;}
for (i=0;i<=lim;i++)
r[i]=((r[i>>1]>>1)|((i&1)<<(cnt-1)));
}
void work()
{
ntt(lim,a,1);int i;
for (i=0;i<lim;i++) a[i]=qpow(a[i],n);
ntt(lim,a,-1);
ll ans=0,tmp=qpow(lim,maxd-2);
for (i=0;i<=N;i++)
{
a[i]=(a[i]*tmp)%maxd;
ans=(ans+1ll*a[i]*a[i])%maxd;
}
printf("%lld",ans);
}
signed main()
{
init();
work();
return 0;
}
codeforces1096G Lucky Tickets的更多相关文章
- 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]\] 其中\ ...
- 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 ...
- URAL1036. Lucky Tickets
链接 dp[i][j] += dp[i-1][j-g];背包吧 数据太大了 还是JAVA好用 import java.io.*; import java.math.*; import java.tex ...
- poj 2346 Lucky tickets(区间dp)
题目链接:http://poj.org/problem?id=2346 思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将 ...
随机推荐
- .net core2 笔记
资源: https://github.com/aspnet/home https://github.com/dotnet/cli https://www.cnblogs.com/billyang/p/ ...
- latex 图片标题居中
1.有一个全局图片标题居中的方法: \usepackage[justification=centering]{caption} 2.如果排版时有的图标题想左对齐,有的想居中,前一个方法就不好了,这里可 ...
- Vue基础(ES6)
起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就 ...
- Linux (Redhat / Fedora / CentOS) 更改 hostname 的方式
Linux (Redhat / Fedora / CentOS) 更改 hostname 的方式 [蔡宗融個人網站]https://www.ichiayi.com/wiki/tech/linux_ho ...
- CMake--模块的使用和自定义模块
1.链接外部库 如果程序中使用了外部库,事先并不知道它的头文件和链接库的位置,就要给出头文件和链接库的查找方法,并将他们链接到程序中. FIND_PACKAGE(<name> [major ...
- HDU 5898 odd-even number
题目:odd-even number 链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5898 题意:给一个条件,问l 到r 之间有多少满足条件的 ...
- 将form数据转换成json对象自定义插件实现思路
- CentOs7安装docker(第二篇)
一.Docker的概念: Docker: 镜像:Images 类似于面向对象中的类 容器:Container 类似于面向对象中的对象 它们之间的关系:容器Container通过镜像Images来创建 ...
- C# Note10: AutoComplete TextBox in WPF
参考: 1.https://stackoverflow.com/questions/950770/autocomplete-textbox-in-wpf 2.AutoCompleteBox的使用(实现 ...
- 移动端Web界面滚动touch事件
解决办法一: elem.addEventListener( 'touchstart', fn, { passive: false } ); 解决办法二: * { touch-action: pan-y ...