【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 ...
随机推荐
- Python开发【内置模块篇】日志模块
logging配置 import logging logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(filename)s ...
- 无post按钮提交表单
<form id="form1" name="form" action="url" method="GET"> ...
- UVALive - 3523 - Knights of the Round Table
Problem UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...
- 通过Excel生成PowerDesigner表结构设计
说明:近期做部分表结构设计,在word里设计调整好了,需要整理到PowerDesigner中,但是手工录入太麻烦. 找了个工具(地址:http://www.cnblogs.com/hwaggLee/p ...
- MATLAB accumarray
先看看subs和val的具体内容 subs = [1 1 1; 2 1 2; 2 3 2; 2 1 2; 2 3 2]; subs = 1 1 1 2 1 2 2 ...
- git 命令积累
git status # 查看仓库的状态 git add . # 监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容修改(modified)以及新文件(new),但不包括被删除的 ...
- iOS开发基础篇-手写控件
一.手写控件的步骤 1)使用相应的控件类创建控件对象: 2)设置该控件的各种属性: 3)添加空间到视图中: 4)如果是 UIButton 等控件,还需考虑控件的单击事件等: 二.添加 UIButton ...
- 【转】详解springboot-修改内置tomcat版本
1.解析Spring Boot父级依赖 <parent> <groupId>org.springframework.boot</groupId> <artif ...
- Django组件 之 分页器(paginator)
--------------------------------------------------------------------------------路虽远,行则将至. 事虽难,做则必成. ...
- js把变量转换成json数据
var a="";var MessageList=JSON.stringify(a);