首页 > 其他分享 >P9821 [ICPC2020 Shanghai R] Sum of Log

P9821 [ICPC2020 Shanghai R] Sum of Log

时间:2023-11-05 18:44:37浏览次数:28  
标签:lg ICPC2020 int max Sum Shanghai pos include sum

原题链接

题意,求:

\[\sum_{i=0}^{X}\sum_{j=[i=0]}^{Y}[i\&j=0]\lfloor\log_2(i+j)+1\rfloor \]

为简洁,记 \(\lg(x)=\lfloor\log_2(x)\rfloor,n=\max(X,Y)\)

由于 \(i\&j=0\) 则 \(i+j=i\operatorname{|}j\) 则 \(\lg(i+j)=\lg(i\operatorname{|}j)=\lg(\max(i,j))\)。

考虑枚举 \(\lg(\max(i,j))\):

\[\sum_{k=0}^{\lg(n)} (k+1)\sum_{i=0}^{X}\sum_{j=0}^{Y}[i\&j=0][\lg(\max(i,j))=k] \]

考虑枚举 \(k\),后面那一坨相当于计数。

考虑 \([i\&j=0]\) 的意义是 \(i\) 与 \(j\) 每一二进制位上不能同时为 \(1\)。

考虑 \([\lg(\max(i,j))=k]\) 的意义是 \(i\) 与 \(j\) 的第 \(k\) 二进制位上必须有且仅有一个数为 \(1\) 且第 \(k\) 位以上全为 \(0\)。

这个东西就可以数位 DP 统计,记 \(f_{pos,0/1,0/1}\) 为考虑第 \(pos\) 位时,\(i,j\) 是否被限制(这个“被限制”类比数位 DP)时的方案数。

状态数 \(O(\log n)\) 级别,转移 \(O(1)\) 级别,时间复杂度 \(O(T\log n)\)。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int MOD=1e9+7;
int T,X,Y;long long f[50][2][2],ANS;
int dfs(int pos,bool a,bool b)//a,b 表示第一个数和第二个数是否被限制
{
    if(pos<0) return 1;
    if(f[pos][a][b]!=-1) return f[pos][a][b];
    long long ans=0;
    for(int x=0;x<=(a?((X>>pos)&1):1);++x)
        for(int y=0;y<=(b?((Y>>pos)&1):1);++y)
        {
            if(x&&y) continue;//不能同时为 1
            ans+=dfs(pos-1,a&(x==((X>>pos)&1)),b&(y==((Y>>pos)&1)));
        }
    return f[pos][a][b]=ans%MOD;
}
int main()
{
#ifdef ONLINE_JUDGE
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(0);
#endif
    cin>>T;
    while(T--)
    {
        cin>>X>>Y;ANS=0;
        memset(f,-1,sizeof(f));
        for(int i=0;i<=30;++i)//枚举上文中的 k
        {
            long long ans=0;
            if((1<<i)<=X) ans+=dfs(i-1,(X-(1<<i))<(1<<i),Y<(1<<i));//使第一个数第 k 位为 1
            if((1<<i)<=Y) ans+=dfs(i-1,X<(1<<i),(Y-(1<<i))<(1<<i));//使第二个数第 k 位为 1
            ANS+=ans*(i+1)%MOD;
        }
        cout<<ANS%MOD<<'\n';
    }
}

标签:lg,ICPC2020,int,max,Sum,Shanghai,pos,include,sum
From: https://www.cnblogs.com/int-R/p/P9821.html

相关文章

  • P1466 [USACO2.2] 集合 Subset Sums
    P1466USACO2.2集合SubsetSums毫无思路如果不告诉我这题是DP题,我一定会爆搜。看了题解,很妙。居然也能套背包板子。定义F[i][j]为在前\(i\)个数中选择一些数其和为\(j\)的方案总数。显然转移方程F[i][j]=F[i-1][j]+F[i-1][j-i]要么不选当前第\(i\)个数,要么选......
  • e1000e 0000:00:1f.6: The NVM Checksum Is Not Valid
    Ubuntu20.04系统,遇到I219网卡不能用的问题,查看dmesg得到如下信息: 解决办法:1.下载Intel官方工具BootUtility: 下载地址:https://www.intel.com/content/www/us/en/download/15755/intel-ethernet-connections-boot-utility-preboot-images-and-efi-drivers.html?下载linu......
  • 单元格内多段数字,TEXTSPLIT结合SUM快速求和!
    1职场实例小伙伴们大家好,今天我们来讲解一个职场办公中经常遇到的问题模型:单元格内,用分隔符间隔开的多段数字,如何实现快速求和?今天我们想要用函数公式的方式实现。如下图所示:A列为一列数据,每个单元格内的数字都是以分隔符号逗号间隔开的,我们想要将单元格内每段数字相加求和,显示在C......
  • 题解 [ARC149B] Two LIS Sum
    题解[ARC149B]TwoLISSum大胆猜结论,按照\(a\)数组为关键字进行排序,求更改后\(b\)的\(LIS\)。证明:每次移动,都有\(a\)中增加一个长度,\(b\)中贡献可能为\(\{-1,0,1\}\),总体贡献为\(\{0,1,2\}\),具体为:\(b\)中排序后大数变到小数前方。\(b\)中排序后小数仍然......
  • kafka复习:(10)按分区获取ConsumerRecord
    packagecom.cisdi.dsp.modules.metaAnalysis.rest.kafka2023;importorg.apache.kafka.clients.consumer.ConsumerConfig;importorg.apache.kafka.clients.consumer.ConsumerRecord;importorg.apache.kafka.clients.consumer.ConsumerRecords;importorg.apache.kafka......
  • PAT_A1081 Rational Sum
    Given N rationalnumbersintheform numerator/denominator,youaresupposedtocalculatetheirsum.InputSpecification:Eachinputfilecontainsonetestcase.Eachcasestartswithapositiveinteger N (≤100),followedinthenextline N rationaln......
  • PAT_A1104 Sum of Number Segments
    Givenasequenceofpositivenumbers,asegmentisdefinedtobeaconsecutivesubsequence.Forexample,giventhesequence{0.1,0.2,0.3,0.4},wehave10segments:(0.1)(0.1,0.2)(0.1,0.2,0.3)(0.1,0.2,0.3,0.4)(0.2)(0.2,0.3)(0.2,0.3,0.4)......
  • 003Square(n)Sum(8kyu)from codewars
    Square(n)SumCompletethesquaresumfunctionsothatitsquareseachnumberpassedintoitandthensumstheresultstogether.完成平方和函数,对每个传入其中的数字平方并相加。defsquare_sum(numbers):sums=0foriinnumbers:sums+=i*iretu......
  • E. Segment Sum
    E.SegmentSumYouaregiventwointegers$l$and$r$($l\ler$).Yourtaskistocalculatethesumofnumbersfrom$l$to$r$(including$l$and$r$)suchthateachnumbercontainsatmost$k$differentdigits,andprintthissummodulo$998244353$.Fo......
  • Leecode 1. 两数之和 Two Sum
    给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。示例1:输入:nums=[2,7,11......