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的数的数目. 将 ...
随机推荐
- React 与 React Native 底层共识:React 是什么
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React 与 React Native 结合学习的方法,此小节主要介绍 React 的底层原理与 ...
- Magic Stones CodeForces - 1110E (思维+差分)
E. Magic Stones time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Python编码与变量
(一)Python执行的方式 Window: 在CMD里面,使用 Python + 相对路径/绝对路径 在解释器里面,直接输入,一行代码一行代码的解释 Linux: 明确地指出用Python解释器来执 ...
- Survey项目总结
1.Ioc深入理解 Inverse of control org.springframework.scheduling.quartz.SchedulerFactoryBean org.mybatis. ...
- PS制作墙壁上海报卷页图片效果
1.首先,打开PS,新建合适的画布. 2.为了使背景具有质感,执行滤镜—滤镜库—纹理化,具体参数按你的感觉来. 3.新建画布“图层1”,为了方便观察,填充为灰色画布,ctrl+t适当缩小画布大小,如图 ...
- iOS-带图片的二维码的生成(QRCode)
https://blog.csdn.net/feng512275/article/details/82824650 2018年09月23日 20:29:45 筝风放风筝 阅读数:91 版权声明:本 ...
- Django 生成验证码或二维码 pillow模块
一.安装PIL PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,API也非常简单易用. PIL模块只支持到Python 2 ...
- 【转】shell之for、while、until循环
一.简介 Shell编程中循环命令用于特定条件下决定某些语句重复执行的控制方式,有三种常用的循环语句:for.while和until.while循环和for循环属于“当型循环”,而unti ...
- [转帖]OS/2 兴 衰 史
OS/2 兴 衰 史 https://zhidao.baidu.com/question/12076254.html 最近在看windows的版本 感觉自己接触电脑太晚 知道的也是很少 不明白 之前有 ...
- 下拉框、下拉控件之Select2
一.Select2的功能简介 select2插件给我们带来了更加友好的交互方式,比如查询控件展开后可通过关键字进行检索 例如: Select2也可以选择带查询控件的选择框... Select2也可以选 ...