【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 ...
随机推荐
- logging_modules
老师的博客:http://www.cnblogs.com/Eva-J/articles/7228075.html#_label13 basicconfig import logging logging ...
- SQL CREATE TABLE 语句
CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据 ...
- MYSQL中文乱码以及character_set_database属性修改
新安装MYSQL,还没有修改数据库系统编码. 之后由于创建数据库时候:create database db_name; 没有指定编码,之后发现乱码就修改各个属性之后还是乱码,便开始配置数据库属性,之后 ...
- C++ SIMD
SIMD Single Instruction Multiple Data
- Dubbo-Zookeeper安装
安装zookeeper: 1.拖入tar.gz包,解压 2.建立/usr/zookeeper路径,该路径创建logs文件夹和data文件夹 3.进入conf目录,复制一份zoo_sample.cfg为 ...
- spring @CrossOrigin解决跨域问题
阅读目录: 一.跨域(CORS)支持: 二.使用方法: 1.controller配置CORS 2.全局CORS配置 3.XML命名空间 4.How does it work? 5.基于过滤器的CORS ...
- 几种c++字符串split 函数实现的比较
文中的字符串split函数功能是 从字符串中按照特定的分隔符进行分割,分割的结果保存到std::vector中. 1. strtok实现 std::vector<std::string> ...
- Educational Codeforces Round 62 (Rated for Div. 2) - C Playlist
当时题意看错了...不过大致思路是对的,唯一没有想到的就是用优先队列搞这个东西,真是不该啊... 题意大概就是,有N首歌,N首歌有两个东西,一个是长度Ti,一个是美丽值Bi,你最多可以选择K首歌, 这 ...
- SpringBoot2.0的CacheManager配置
http://rickgong.iteye.com/blog/2414263 @Configurationpublic class RedisConfig extends CachingConfigu ...
- 根据 Power BI Desktop(预览版)中的报表页创建工具提示
根据 Power BI Desktop 中创建的报表页,可创建直观丰富的报表工具提示,这些提示在你将鼠标悬停在视觉对象上时显示. 通过创建用作工具提示的报表页,使自定义工具提示包含视觉对象.图像以及在 ...