You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need to fully partition the sequence into M chunks. Each chunk must be a consecutive subsequence of the original sequence. Let Si (1 ≤ i ≤ M) be the total number of 1's in all numbers in the ith chunk when written in binary, and let S be the maximum of all Si, i.e. the maximum number of 1's in any chunk. Your goal is to minimize S.

Input

In the first line of input, two numbers, K and M (1 ≤ K ≤ 100, 1 ≤ M ≤ 100, M ≤ 2^K), are given, separated by a single space character.

Output

In one line of the output, write the minimum S that can be obtained by some split. Write it without leading zeros. The result is not guaranteed to fit in a 64-bit integer.

Example

Input:

3 4

Output:

4

题解:

from 算法合集之《浅谈数位类统计问题》

code:

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxn=;
int k,n;
const int MAXN=;
const int maxnum=;
struct bignum{
int len,v[MAXN];
bignum(){memset(v,,sizeof(v)),len=;}
bignum operator=(const char* num){
memset(v,,sizeof(v));
len=((strlen(num)-)>>)+;
int j=,k=;
for (int i=strlen(num)-;i>=;i--){
if (j==maxnum) j=,k++;
v[k]+=(num[i]-'')*j;
j*=;
}
return *this;
}
bignum operator=(const int num){
char a[MAXN<<];
sprintf(a,"%d",num);
*this=a;
return *this;
}
bignum (int num){*this=num;}
bignum (const char* num){*this=num;}
bignum operator+(const bignum &a){
bignum c;
c.len=max(len,a.len);
for (int i=;i<c.len;i++){
c.v[i]+=v[i]+a.v[i];
if (c.v[i]>=maxnum) c.v[i+]+=(c.v[i]/maxnum),c.v[i]%=maxnum;
}
while (c.v[c.len]) c.len++;
return c;
}
bignum operator-(const bignum b){
bignum a,c;
a=*this;
c.len=len;
for (int i=;i<len;i++){
if (a.v[i]<b.v[i]) a.v[i+]--,a.v[i]+=maxnum;
c.v[i]=a.v[i]-b.v[i];
}
while (c.len>&&!(c.v[c.len-])) c.len--;
return c;
}
bignum operator*(const bignum &a){
bignum c;
c.len=len+a.len;
for (int i=;i<len;i++)
for (int j=;j<a.len;j++){
c.v[i+j]+=v[i]*a.v[j];
if (c.v[i+j]>=maxnum) c.v[i+j+]+=(c.v[i+j]/maxnum),c.v[i+j]%=maxnum;
}
while (c.len>&&!(c.v[c.len-])) c.len--;
return c;
}
bignum operator*(const int &a){
bignum c=a;
return *this*c;
}
bignum operator/(const int &b){
bignum c;
int x=;
for (int i=len-;i>=;i--){
c.v[i]=(x*maxnum+v[i])/b;
x=(x*maxnum+v[i])%b;
}
c.len=len;
while (c.len>&&!(c.v[c.len-])) c.len--;
return c;
}
bignum operator+=(const bignum &a){*this=*this+a;return *this;}
bignum operator-=(const bignum &a){*this=*this-a;return *this;}
bignum operator*=(const bignum &a){*this=*this*a;return *this;}
bignum operator/=(const int &a){*this=*this/a;return *this;}
bool operator < (const bignum &x)const{
if (len!=x.len) return len<x.len;
for (int i=len-;i>=;i--)
if (v[i]!=x.v[i]) return v[i]<x.v[i];
return false;
}
bool operator > (const bignum &x)const{return x<*this;}
bool operator <=(const bignum &x)const{return !(x<*this);}
bool operator >=(const bignum &x)const{return !(*this<x);}
bool operator ==(const bignum &x)const{return !(x<*this||*this<x);}
bool operator !=(const bignum &x)const{return x<*this||*this<x;}
};
void write(bignum x){
printf("%d",x.v[x.len-]);
for (int i=x.len-;i>=;i--) printf("%0*d",,x.v[i]);
puts("");
}
void read(bignum &x){
static char num[MAXN<<];
scanf("%s",num);
x=num;
}
bignum l,r,m,pw2[maxn],sum[maxn],tmp,res,t,xx;
int a[maxn];
void init(){
pw2[]=;
for (int i=;i<=;i++) pw2[i]=pw2[i-]*;
sum[]=;
for (int i=;i<=;i++) sum[i]=sum[i-]*+pw2[i-];
}
void calc(){
tmp=,res=;
for (int i=;i<=k;i++) if (a[i]) res+=tmp+sum[i-],tmp+=pw2[i-];
}
void find(bignum lim){
bool flag=(lim>=sum[k]);
int tmp=;
for (int i=k;i>=;i--){
xx=sum[i-]+pw2[i-]*tmp;
if (lim>=xx) lim=lim-xx,tmp++,a[i]=;
else a[i]=;
}
if (!flag){
for (int i=;i<=k;i++){
a[i]^=;
if (!a[i]) break;
}
}
}
bool check(){
for (int i=;i<=k;i++) if (!a[i]) return false;
return true;
}
bool check(bignum lim){
bignum t=;
int cnt=n;
memset(a,,sizeof(a));
while (cnt--){
find(t+lim),calc(),t=res;
if (check()) return true;
}
return false;
}
int main(){
init();
read(k),read(n);
l=,r=sum[k];
while (l!=r){
m=(l+r)/;
if (check(m)) r=m; else l=m+;
}
write(l);
return ;
}

spoj 2319 BIGSEQ - Sequence的更多相关文章

  1. 【SPOJ】2319 BIGSEQ - Sequence

    [算法]数位DP [题解]动态规划 题目要求的是大整数……没办法只写了小数字的,感觉应该没错. 大题框架是最大值最小化的二分问题. 对于每一块要求count(b)-count(a-1)≥s 已知a如何 ...

  2. 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)

    BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...

  3. [SPOJ SEQN] [hdu3439]Sequence

    题目就是求C(n,k)*H(n - k)%m 0<= k<= n <=10^9, 1 <= m <= 10^5, n != 0 其中H(n)是错排第n项. 对于C(n,k ...

  4. 【专题】数位DP

    [资料] ★记忆化搜索:数位dp总结 之 从入门到模板 by wust_wenhao 论文:浅谈数位类统计问题 数位计数问题解法研究 [记忆化搜索] 数位:数字从低位到高位依次为0~len-1. 高位 ...

  5. [DP]数位DP总结

     数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step   http://blog.csdn.net/dslovemz/article/details/ ...

  6. 【spoj SEQN】【hdu 3439】Sequence

    题意: 给出n.m.k 求C(n,k)*H(n-k)%m的值 H(n-k)为错排公式 题解: 先算H(n-k) 计算H(n)有个通式: H(n)=(-1)^n+((-1)^(n-1))n+((-1)^ ...

  7. 【SPOJ】1182 Sorted bit sequence

    [算法]数位DP [题解]动态规划 写了预处理函数却忘了调用是一种怎样的体验? #include<cstdio> #include<cstring> #include<a ...

  8. SPOJ 1182 Sorted bit sequence

    题目链接 题意: 分析: 其实如果会了Ural 1057. Amount of Degrees那道题目,这道题自然也就会了... 我们考虑枚举第$k$个数字的$1$的个数,那么我们需要计算的也就是区间 ...

  9. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

随机推荐

  1. 帮助招聘程序员的自动考试网站:Codility

    Automated tests of programming skills. Assessment of software developers. Recruitment software. Codi ...

  2. C#和java和android中的NetWorkAdapter,httpRequest,WebView,json,xml

    原文地址:http://blog.csdn.net/intbird C#NetWorkAdapter 20121011.======================================== ...

  3. CFS: 虚拟运行时间

    http://edsionte.com/techblog/archives/4331 nice和prio的关系如下: #define NICE_TO_PRIO(nice) (MAX_RT_PRIO+n ...

  4. curl post参数,接口接收不到数据问题

    今天遇到一个问题,注册下发短信失败,总提示无法发送注册短信,请从新发送. 经检查,curl里面将post数据以json_encode的方法转码之后传递,而且各选项设置感觉没有问题,怎么接口就接收不到p ...

  5. Driving the Activity Lifecycle

    Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActiv ...

  6. 第三篇:python高级之生成器&迭代器

    python高级之生成器&迭代器   python高级之生成器&迭代器 本机内容 概念梳理 容器 可迭代对象 迭代器 for循环内部实现 生成器 1.概念梳理 容器(container ...

  7. 如何使用node中的buffer

    介绍:Buffer类是一个全局类,是一个比较罕见不需要require( ‘buffer’ )就可以使用的类,Buffer类似与数组也有length, 它里面的元素为16进制的两位数,即 0-255的数 ...

  8. 【转】纯 CSS 实现高度与宽度成比例的效果

    先来演示页面:Demo; 转的内容: 最近在做一个产品列表页面,布局如右图所示.页面中有若干个 item,其中每个 item 都向左浮动,并包含在自适应浏览器窗口宽度的父元素中. item 元素的 C ...

  9. Creating a web application.

    About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...

  10. 部分A+B_1

    正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...