首页 > 其他分享 >Solution of Codeforces 1957B

Solution of Codeforces 1957B

时间:2024-04-28 12:13:43浏览次数:10  
标签:10 le 1957B sequence Solution mid Codeforces find

Description

Given integers \(n\) and \(k\), find a non-negative sequence \(\{a_n\}\) satisfying the following conditions:
1.

\[\sum_{i=1}^n a_i = k \]

  1. The binary representation of \(a_1\mid a_2\mid \dots\mid a_n\) has the maximum number of \(1\)s, where \(\mid\) represents the bitwise OR operation.

Multiple tests.

Data Range: \(1\leq n\leq 2\cdot 10^5\), \(1\le k\le 10^9\), \(1\le t\le 10^4\).

Solution

For \(n=1\), there is only one way to construct the sequence, that is to output \(k\) itself.

For \(n>1\), We can easily find out that the answer must be less or equal than \(\log_2 k\), therefore, we only need to find the number of bits in the binary representation of \(k\) (denoted by \(b\)), then the sequence is constructed by:

\(2^{b-1}, k - 2^{b-1}\) and \(n-2\) of \(0\)s.

标签:10,le,1957B,sequence,Solution,mid,Codeforces,find
From: https://www.cnblogs.com/hl-fc/p/18163431

相关文章

  • Codeforces Round 936 (Div. 2)
    A考虑有\(x\)个数与中位数相同,且在中位数之后,则答案为\(x+1\)(+1是因为中位数本身)。B明显的,每次操作序列的最大子段和。那么操作完以后,继续操作这个区间即可,相当于每次翻倍。假设原序列最大子段和为区间\([l,r]\),则答案为:\[sum(1,n)-sum(l,r)+sum(l,r)\times2^k\]记......
  • Codeforces Round 931 (Div. 2) D2
    挺简单的题目,就是一个普通博弈论套了一个交互的皮。。。但是恶心的就是,交互的皮是真难顶啊,什么sb玩意,我因为爆了int一直给我现实TLEontest1,这我怎么调?不得不说,交互题和普通的题目真是差别太大了,就评测这一块,返回的消息完全无法给我有效的指导。。然后我就直接坐大牢。要是这......
  • Codeforces Round 939 (Div. 2) E1-E2
    CodeforcesRound939(Div.2)E1.Nenevs.Monsters(EasyVersion)题意:有n个怪物,能量等级为\(a_i\),现在可以使用一种法术,使第1个怪物攻击第2个怪物,第2个怪物攻击第3个怪物……第n个怪物攻击第1个怪物,当能量等级为x的怪物攻击能量等级为y的怪物时,怪物y->max(y-x,0),在经过\(1......
  • Codeforces Round 892 (Div. 2) E
    E的话一眼dp,然后观察一下方程,\(f[i][j]表示前i个位置已经选了长度为j的区间,且第i个位置已经被选上时,能够获得的最大值\)\[f[i][j]=\displaystyle\max_{1\leqk\leqmin(i,j)}(f[i-k][j-k]+calc(i-k+1,j))\\calc(l,r)=|b_l-a_r|+|b_r-a_l|\]这样的dp是\(O(n^2k)\)的,而\(1\leqk......
  • cf 1601B Frog Traveler Codeforces Round 751 (Div. 1)
     Problem-1601B-Codeforces BFS然后每次上升可以的范围是一个区间,然后每次都遍历这个区间的所有点,那么超时。用set等方式,合并这些区间,之前没遍历过的范围才更新(加入BFS需要遍历的队列里)。但是区间的更新特别容易写错…… 我的代码和造数据1/**2记录两个vi......
  • Codeforces Round 940 (Div. 2) and CodeCraft-23 题解
    CodeforcesRound940(Div.2)andCodeCraft-23题解题目链接A.Stickogon贪心#include<bits/stdc++.h>usingnamespacestd;#definefffirst#definesssecond#definepbpush_back#defineall(u)u.begin(),u.end()#defineendl'\n'#definedebu......
  • Educational Codeforces Round 164 (Rated for Div. 2)
    A.PaintingtheRibbon题意:n个物体,m个颜色,alice要给每个物体任意涂一个颜色。bob可以给k个物体涂色,问alice能否阻止bob让所有的物体颜色都相同。思路:思维题。如果m=1,那么无解。如果m>1,那么bob如果想要染成同一个颜色,alice可以让bob需要染色的数量最多。如果染色的数量最多,那......
  • Codeforces Round 906 (Div. 2) E1
    时隔了不知道多久的补题。两个月吧,这是可是寒假的比赛。但是补题的时候还是遇到了很多问题。很重要的有一些地方能够简化,一些条件没有充分的利用上,导致了我很多地方考虑的太复杂。这些能够简化的地方全部利用上我觉得才算是写出来了这道题目,否则这题会复杂到我赛时写不完,而且特......
  • CodeForces 115D Unambiguous Arithmetic Expression
    洛谷传送门CF传送门直接区间dp可以做到\(O(n^3)\),卡常可过,在此就不赘述了。为了方便先把连续的数字缩成一段。我们考虑直接从前往后扫,扫的过程中dp。设\(f_{i,j}\)为考虑了\([1,i]\),还有\(j\)个没配对的左括号的方案数。但是我们发现我们不知道一个数字前要添加几......
  • Codeforces Round 940 (Div. 2) and CodeCraft-23
    A.Stickogon#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;#defineinti64usingvi=vector<int>;constintN=3e5,mod=1e9+7;voidsolve(){ vicnt(101); intn; cin>>n; for(i......