【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 ...
随机推荐
- Autofs自动挂载探讨
Autofs介绍: mount是用来挂载文件系统的,可以在系统启动的时候挂载也可以在系统启动后挂载.对于本地固定设 备,如硬盘可以使用mount挂载:而光盘.软盘.NFS.SMB等文件系统具有动态性, ...
- MySQL常用日期时间函数
日期和时间函数: MySQL服务器中的三种时区设置: ①系统时区---保存在系统变量system_time_zone ②服务器时区---保存在全局系统变量global.time_zone ③每个客户端 ...
- 文本分类实战(十)—— BERT 预训练模型
1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...
- 【转】Android中保持Service的存活
这几天一直在准备考试,总算有个半天时间可以休息下,写写博客. 如何让Service keep alive是一个很常见的问题. 在APP开发过程中,需要Service持续提供服务的应用场景太多了,比如闹 ...
- iOS开发基础-图片切换(3)之属性列表
延续:iOS开发基础-图片切换(2),对(2)里面的代码用属性列表plist进行改善. 新建 Property List 命名为 Data 获得一个后缀为 .plist 的文件. 按如图修改刚创建的文 ...
- JQuery 获取select 的value值和文本值
<select name="month" id="selMonth"> <option value="1">一 ...
- python部署galery集群
galery.py文件内容 import pexpect import os import configparser HOSTNAME_DB1='db1' HOSTNAME_DB2='db2' HOS ...
- jmeter学习记录--07--jmeter元件
通过jmeter元件可以模拟负载.参数化.设置关联.设置检查点.设置集合点.控制场景运行.监控测试结果等. 1.逻辑控制器:比如foreach控制器,查询到了订单并要对每个订单进行出库操作,以订单号作 ...
- kettle表更新/插入更新
更新: 1.1更新表: 目标表: 插入更新: 2.1匹配表: 目标表: 插入/更新转换 目标表
- Python——sys模块
七.sys模块 sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传递参数. sys.exit([arg]): 程序中间的退出,arg=0为正常退出. sys.getdefaulten ...