Codeforces-916B:Jamie and Binary Sequence (changed after round)(思维)
2018-01-20 11:33
671 查看
B. Jamie and Binary Sequence (changed after round)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know how to solve it. Help him write a solution to the following problem:
Find k integers such that the sum of two to the power of each number equals to the number n and
the largest integer in the answer is as small as possible. As there may be multiple answers, you are asked to output the lexicographically largest one.
To be more clear, consider all integer sequence with length k (a1, a2, ..., ak) with
.
Give a value
to
each sequence. Among all sequence(s) that have the minimum y value, output the one that is the lexicographically largest.
For definitions of powers and lexicographical order see notes.
Input
The first line consists of two integers n and k (1 ≤ n ≤ 1018, 1 ≤ k ≤ 105) —
the required sum and the length of the sequence.
Output
Output "No" (without quotes) in a single line if there does not exist such sequence. Otherwise, output "Yes"
(without quotes) in the first line, and k numbers separated by space in the second line — the required sequence.
It is guaranteed that the integers in the answer sequence fit the range [ - 1018, 1018].
Examples
input
output
input
output
input
output
Note
Sample 1:
23 + 23 + 22 + 21 + 20 = 8 + 8 + 4 + 2 + 1 = 23
Answers like (3, 3, 2, 0, 1) or (0, 1, 2, 3, 3) are
not lexicographically largest.
Answers like (4, 1, 1, 1, 0) do not have the minimum y value.
Sample 2:
It can be shown there does not exist a sequence with length 2.
Sample 3:
Powers of 2:
If x > 0, then 2x = 2·2·2·...·2 (x times).
If x = 0, then 2x = 1.
If x < 0, then
.
Lexicographical order:
Given two different sequences of the same length, (a1, a2, ...
, ak) and (b1, b2, ...
, bk), the first one is smaller than the second one for the lexicographical order, if and only if ai < bi,
for the first i where ai and bi differ.
思路:我们可以先将n转化为二进制数,然后把高位的1转化低位的2个1,来满足总共有k个1。因为要保证最高位越小越好,一开始就不断从高位向低位转化。若再转化,1的个数将大于k时,就从低位开始一个一个转化,使得字典序尽量大。
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know how to solve it. Help him write a solution to the following problem:
Find k integers such that the sum of two to the power of each number equals to the number n and
the largest integer in the answer is as small as possible. As there may be multiple answers, you are asked to output the lexicographically largest one.
To be more clear, consider all integer sequence with length k (a1, a2, ..., ak) with
.
Give a value
to
each sequence. Among all sequence(s) that have the minimum y value, output the one that is the lexicographically largest.
For definitions of powers and lexicographical order see notes.
Input
The first line consists of two integers n and k (1 ≤ n ≤ 1018, 1 ≤ k ≤ 105) —
the required sum and the length of the sequence.
Output
Output "No" (without quotes) in a single line if there does not exist such sequence. Otherwise, output "Yes"
(without quotes) in the first line, and k numbers separated by space in the second line — the required sequence.
It is guaranteed that the integers in the answer sequence fit the range [ - 1018, 1018].
Examples
input
23 5
output
Yes 3 3 2 1 0
input
13 2
output
No
input
1 2
output
Yes -1 -1
Note
Sample 1:
23 + 23 + 22 + 21 + 20 = 8 + 8 + 4 + 2 + 1 = 23
Answers like (3, 3, 2, 0, 1) or (0, 1, 2, 3, 3) are
not lexicographically largest.
Answers like (4, 1, 1, 1, 0) do not have the minimum y value.
Sample 2:
It can be shown there does not exist a sequence with length 2.
Sample 3:
Powers of 2:
If x > 0, then 2x = 2·2·2·...·2 (x times).
If x = 0, then 2x = 1.
If x < 0, then
.
Lexicographical order:
Given two different sequences of the same length, (a1, a2, ...
, ak) and (b1, b2, ...
, bk), the first one is smaller than the second one for the lexicographical order, if and only if ai < bi,
for the first i where ai and bi differ.
思路:我们可以先将n转化为二进制数,然后把高位的1转化低位的2个1,来满足总共有k个1。因为要保证最高位越小越好,一开始就不断从高位向低位转化。若再转化,1的个数将大于k时,就从低位开始一个一个转化,使得字典序尽量大。
#include<bits/stdc++.h> using namespace std; const int MAX=1e6; const int MOD=1e9+7; typedef __int64 ll; ll p[100],f[MAX];//f[i]表示2^(-i); int main() { ll n,k,res=1; cin>>n>>k; int len=0,last=0; while(n)p[len++]=n%2,n/=2; for(int i=0;i<len;i++)k-=p[i]; if(k<0){puts("No");return 0;}//1的个数一开始就大于k for(int i=len-1;;i--) { if((i>=0&&p[i]==0)||(i<0&&f[-i]==0))continue; if((i>=0&&p[i]>k)||(i<0&&f[-i]>k))break;//再转化,1的个数将大于k if(i>0) { p[i-1]+=2*p[i]; k-=p[i]; p[i]=0; } if(i==0) { f[1]+=2*p[i]; k-=p[i]; p[i]=0; last=1; } if(i<0) { f[-(i-1)]+=2*f[-i]; k-=f[-i]; f[-i]=0; last=-(i-1); } } while(k) { for(int i=last;i>0;i--)//从低位开始 { if(f[i]==0)continue; k--;//一个一个的转化 f[i]-- c314 ; f[i+1]+=2; last=i+1; goto nex; } if(p[0]) { k--; p[0]--; f[1]+=2; last=1; continue; } for(int i=1;i<len;i++) { if(p[i]==0)continue; k--; p[i]--; p[i-1]+=2; goto nex; } nex:; } puts("Yes"); for(int i=len-1;i>=0;i--)while(p[i]>0&&p[i]--)printf("%d ",i); for(int i=1;i<=last;i++)while(f[i]>0&&f[i]--)printf("%d ",-i); return 0; }
相关文章推荐
- Codeforces Round #457 (Div. 2) B. Jamie and Binary Sequence (changed after round)
- Codeforces Round #457 (Div. 2) B. Jamie and Binary Sequence (changed after round)贪心
- Codeforces 916B-Jamie and Binary Sequence
- 【Codeforces】-#222A-KShooshuns and Sequence(思维)
- Codeforces-Round 174(Cows and Sequence)(树状数组、高校算法)
- codeforces 916 A Jamie and Alarm Snooze
- Codeforces400E - Inna and Binary Logic - 思维、数学
- Codeforces 916 C. Jamie and Interesting Graph (构造)
- 【Codeforces Round #457 (Div. 2) B】Jamie and Binary Sequence
- Codeforces 284C Cows and Sequence【思维】
- Codeforces 894C - Marco and GCD Sequence 【GCD+思维】
- Codeforces Round #457 (Div. 2)——B. Jamie and Binary Sequence(贪心)
- Codeforces-743B-Chloe and the sequence(二进制思维题)
- Codeforces Round #457 (Div. 2) B. Jamie and Binary Sequence(思路+模拟)
- hdu 5504 GT and sequence(思维,逻辑,脑洞)
- CodeForces-VK Cup 2012 Round 2 (Unofficial Div. 2 Edition) -C. Substring and Subsequence
- CodeForces - 748C Santa Claus and Robot(思维)
- Codeforces Round #250 (Div. 1) D. The Child and Sequence
- CodeForces 314 B.Sereja and Periods 思维+简单dp【转】
- codeforces438 D The Child and Sequence