【hdu 5632】Rikka with Array
Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
Yuta has an array A of length n,and the ith element of A is equal to the sum of all digits of i in binary representation. For example,A[1]=1,A[3]=2,A[10]=2.
Now, Yuta wants to know the number of the pairs (i,j)(1≤i<j≤n) which satisfy A[i]>A[j].
It is too difficult for Rikka. Can you help her?
Input
The first line contains a number T(T≤10)——The number of the testcases. For each testcase, the first line contains a number n(n≤10300).
Output
For each testcase, print a single number. The answer may be very large, so you only need to print the answer modulo 998244353.
Sample Input
1 10
Sample Output
7
题意:给定一个数$n(n\leq 10^{300})$,问有多少个数对$(i , j)$,满足$1\leq i<j \leq n$且$f[i]>f[j]$,$f[x]$为$x$二进制表示下$1$的个数。
分析:(在打模拟赛时写到的题目……好像写了一种跟所有人都不一样的写法)
首先考虑一个数$x$,我们需要统计满足$1\leq i<x$且$f[i]>f[x]$的$i$的个数。考虑数位dp,将$x$转为二进制形式,从低位往高位推。假设当前在第$i$位,从第$1$位到第$i$位共有$k$个$1$:若当前位为$0$,则直接跳过进行下一位的统计;否则钦定当前要统计进答案的数字的比第$i$位高的位置与$x$相同,且第$i$位为$0$,则此时最低的第$i-1$位至少要有$k+1$个$1$,可任意选取,即需要统计进答案里的方案数为$\sum _{j=k+1}^{i-1} \binom{i-1}{j}$ ,令$s(i,j)=\sum _{d=0}^{j}\binom{i}{d}$,则公式简化为$s(i-1,i-1)-s(i-1,k)$。
现在我们需要统计总答案,且因为$n$很大,无法直接枚举。考虑将$n$转成二进制形式,共有$cnt$位,$a_{i}$为$n$在二进制下第$i$位上的数字。统计每一个$s(i-1,i-1)-s(i-1,k)$被统计进答案的贡献。若$s(i-1,i-1)-s(i-1,k)$会在数字$x$时被统计进答案里,$x$需要满足以下几个条件:1.$1\leq x\leq n$,2. $x$的第$i$位为$1$,3.$x$的前$i$位恰好有$k$个$1$。答案转化为统计满足条件的$x$的个数。
我们递推一个数组$f$,$f(i,j)$表示数值小于等于$n$最低的$i$位,且二进制下恰好含有$j$个$1$的数字的方案数。可得:
$$f(i,j)=\begin{cases}f(i-1,j)~~~~~~~~~~~~~~~~~~~~~~~(a_{i}=0)\\f(i-1,j-1)+\binom{i-1}{j}~~~(a_{i}=1)\end{cases}$$
特殊的,$f(i,0)=1(0\leq i\leq cnt)$。然后就可以数位dp出对于每一个$(i-1,k)$的组合,所有符合条件的数$x$了。
枚举当前在第$i$位,前$i-1$位总共有$k$个$1$,我们令$num=\sum _{d=i+1}^{cnt} 2^{d-(i+1)}\cdot a_{d}$,即大于第$i$位的部分的$0$到$num-1$的方案,则$s(i-1,i-1)-s(i-1,k+1)$的系数$t$计算方式如下:
$$t=\begin{cases}num\cdot \binom{i-1}{k}~~~~~~~~~~~~~~~~~~~~~~~~~(a_{i}=0)\\num\cdot \binom{i-1}{k}+f(i-1,k)~~~(a_{i}=1)\end{cases}$$
然后就可以得到最终的答案了。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int N=1e3+;
const int mod=;
int T,n,cnt,ans,tmp,num,now,t;
int x[N],a[N],C[N][N],s[N][N],f[N][N];
char ch[N];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
void Mod(int& a,int b){a+=b;if(a>=mod)a-=mod;}
int main()
{
for(int i=;i<=;i++)C[i][]=;
for(int i=;i<=;i++)
for(int j=;j<=i;j++)
C[i][j]=(C[i-][j]+C[i-][j-])%mod;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(j)s[i][j]=(s[i][j-]+C[i][j])%mod;
else s[i][j]=C[i][j];
T=read();
while(T--)
{
cnt=ans=;
scanf("%s",ch+);
n=strlen(ch+);
for(int i=;i<=n;i++)x[n-i+]=ch[i]-'';
if(n==&&(x[]==||x[]==)){printf("0\n");continue;}
while(n)
{
if(x[]&)a[++cnt]=,x[]--;
else a[++cnt]=;
for(int i=n;i>=;i--)
if(x[i]&)x[i]/=,x[i-]+=;
else x[i]/=;
while(n&&x[n]==)n--;
}
memset(f,,sizeof(f));
for(int i=;i<=cnt;i++)f[i][]=;
for(int j=;j<=cnt;j++)
for(int i=j;i<=cnt;i++)
if(!a[i])Mod(f[i][j],f[i-][j]);
else
{
Mod(f[i][j],f[i-][j-]);
Mod(f[i][j],C[i-][j]);
}
for(int i=;i<=cnt;i++)
{
num=;
for(int j=cnt;j>i;j--)num=(num*+a[j])%mod;
for(int j=;j<i;j++)
{
t=1ll*num*C[i-][j]%mod;
Mod(ans,1ll*(s[i-][i-]-s[i-][j+]+mod)%mod*t%mod);
if(!a[i])continue;
Mod(ans,1ll*(s[i-][i-]-s[i-][j+]+mod)%mod*f[i-][j]%mod);
}
}
printf("%d\n",ans);
}
return ;
}
【hdu 5632】Rikka with Array的更多相关文章
- 【hdu 6089】Rikka with Terrorist
题意 有一个 \(n\times m\) 的二维网格,其中有 \(k\) 个禁止点. 有 \(q\) 组询问,每组询问为给一个点,求有多少个矩形以这个点为一角且不包含禁止点. \(n,m,k,q\le ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
随机推荐
- 日志学习系列(四)——NLog实例
具体不想介绍了,新建一个解决方案 ,直接用NuGet安装就行了 具体项目代码可以在https://github.com/qiuxianhu/SimpleNLog下载
- 【题解】P2324 [SCOI2005]骑士精神
·有关IDA* 是带有估值函数的迭代加深搜索,表现出出色的效率. 估值函数可以简单的定义为「已经到位的骑士的个数」. 然后就是普通的迭代加深了. 算法酷炫不一定赢,搜索好才是成功. ——Loli Co ...
- 消耗CPU和内存的脚本
用法 ./shell.sh 4 (4为4内核) 查看cpu内核数量 > lscpu 执行后会出现一堆kill命令,方便kill掉进程 #!/bin/bash endless_loop() { e ...
- Linux内核入门到放弃-虚拟文件系统-《深入Linux内核架构》笔记
VFS的任务并不简单.一方面,它用来提供了一种操作文件.目录及其他对象的统一方法.另一方面,它必须能够与各种方法给出的具体文件系统的实现达成妥协,这些实现在具体细节.总体设计方面都有一些不同之处. 文 ...
- mybatis从mapper接口跳转到相应的xml文件的eclipse插件
mybatis从mapper接口跳转到相应的xml文件的eclipse插件 前提条件 开发软件 eclipse 使用框架 mybatis 为了方便阅读源码,项目使用mybatis的时候,方便从mapp ...
- IdentityServer4【Topic】Consent
Conset这个概念在Identityserver4中是表示要当前用户对第三方应用对资源请求的一个确认,它会被做成一个页面. 术语映射: Consent page--确认页面,我喜欢叫做Consent ...
- Ajax提交表单初接触
<!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...
- SpringCloud学习笔记:SpringCloud简介(1)
1. 微服务 微服务具有的特点: ◊ 按照业务划分服务 ◊ 每个微服务都有独立的基础组件,如:数据库.缓存等,且运行在独立的进程中: ◊ 微服务之间的通讯通过HTTP协议或者消息组件,具有容错能力: ...
- 【NLP】Conditional Language Modeling with Attention
Review: Conditional LMs Note that, in the Encoder part, we reverse the input to the ‘RNN’ and it per ...
- Django ORM操作补充
操作补充 only 只取某些去除其他 defer 去除某些取其他 # 需求: 只取某n列 queryset=[ {},{}] models.User.objects.all().values( 'id ...