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的更多相关文章

  1. 【hdu 6089】Rikka with Terrorist

    题意 有一个 \(n\times m\) 的二维网格,其中有 \(k\) 个禁止点. 有 \(q\) 组询问,每组询问为给一个点,求有多少个矩形以这个点为一角且不包含禁止点. \(n,m,k,q\le ...

  2. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  3. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  4. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  5. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  6. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  7. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  8. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  9. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

随机推荐

  1. 打印进度条>>>>

    i+1: 当前的数量 300:  总数量 import sys print("下载中...") def process(curr, count): cursor_count = c ...

  2. .NET CORE学习笔记系列(2)——依赖注入【2】基于IoC的设计模式

    原文:https://www.cnblogs.com/artech/p/net-core-di-02.html 正如我们在<控制反转>提到过的,很多人将IoC理解为一种“面向对象的设计模式 ...

  3. Python开发【前端篇】JavaScript和Jquery

    JavaScript嵌入页面的方式 1.行间事件(主要用于事件) <input type="button" name="" onclick="a ...

  4. Vue项目分环境打包的实现步骤

    转:https://blog.csdn.net/xinzi11243094/article/details/80521878 方法一:亲测真的有效 在项目开发中,我们的项目一般分为开发版.测试版.Pr ...

  5. YAML详解

    1   YAML简介 YAML,即YAML Ain’t Markup Language的缩写,YAML 是一种简洁的非标记语言.YAML以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁 ...

  6. Java高级篇(四)——反射

    之前写到了设计模式的代理模式,因为下一篇动态代理等内容需要用到反射的知识,所以在之前Java篇的基础上再写一篇有关反射的内容,还是以实际的程序为主,了解反射是做什么的.应该怎么用. 一.什么是反射 反 ...

  7. 使用sz/rz基于串口传输文件

    关键词:lrzsz.minicom.ZMODEM.MD5sum等. 在环境受限的嵌入式系统上,往往只有串口可以使用. 此时如果需要传输文件,需要借助rz/sz工具,可以使用的传输协议有ZMODEM.Y ...

  8. 调参必备---GridSearch网格搜索

    什么是Grid Search 网格搜索? Grid Search:一种调参手段:穷举搜索:在所有候选的参数选择中,通过循环遍历,尝试每一种可能性,表现最好的参数就是最终的结果.其原理就像是在数组里找最 ...

  9. JS--编码规范

    1. 请修复给定的 js 代码中,函数定义存在的问题 function functions(flag) { if (flag) { function getValue() { return 'a'; ...

  10. 【学习总结】GirlsInAI ML-diary day-14-function函数

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day14 认识函数function 函数相当于一个固定的公式,一个映射.有输入,有输出. 1-python内置函数 max ...