spoj 2319 BIGSEQ - Sequence
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的更多相关文章
- 【SPOJ】2319 BIGSEQ - Sequence
[算法]数位DP [题解]动态规划 题目要求的是大整数……没办法只写了小数字的,感觉应该没错. 大题框架是最大值最小化的二分问题. 对于每一块要求count(b)-count(a-1)≥s 已知a如何 ...
- 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...
- [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 ...
- 【专题】数位DP
[资料] ★记忆化搜索:数位dp总结 之 从入门到模板 by wust_wenhao 论文:浅谈数位类统计问题 数位计数问题解法研究 [记忆化搜索] 数位:数字从低位到高位依次为0~len-1. 高位 ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
- 【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)^ ...
- 【SPOJ】1182 Sorted bit sequence
[算法]数位DP [题解]动态规划 写了预处理函数却忘了调用是一种怎样的体验? #include<cstdio> #include<cstring> #include<a ...
- SPOJ 1182 Sorted bit sequence
题目链接 题意: 分析: 其实如果会了Ural 1057. Amount of Degrees那道题目,这道题自然也就会了... 我们考虑枚举第$k$个数字的$1$的个数,那么我们需要计算的也就是区间 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
随机推荐
- list去除重复
1. [代码][Python]代码 简单去重 ? 1 2 3 4 5 l = [1,2,3,3] l = list(set(l)) >>>l >>>[ ...
- HTML5 canvas标签绘制正三角形 鼠标按下点为中间点,鼠标抬起点为其中一个顶点
用html5的canvas标签绘制圆.矩形比较容易,绘制三角形,坐标确定相当于前面两种难点,这里绘制的是正三角形,比较容易,我们只需要把鼠标刚按下去的点设置为三角形的中心点,鼠标抬起的点设置为三角形右 ...
- DataTable无法使用AsEnumerable ()的解决办法
本人定义了DataSet后将表1赋给datatable,在写linq时调用datatable.asenumerable(),但报datatable不包含asenumerable的定义,求高手指点.Sy ...
- PHP运行出现Notice : Use of undefined constant 的解决方法【已测】
关闭 PHP 提示的方法 搜索php.ini:error_reporting = E_ALL改为:error_reporting = E_ALL & ~E_NOTICE还有个不是办法的办法就是 ...
- nanosleep纳秒级延迟
//函数原型 int nanosleep(struct timespec *req, struct timespec *rem) //参数列表: // req:要求的睡眠时间 // rem:剩余的睡眠 ...
- 高级性能调试手段(oprofile+gprofile)+内核追踪手段:LTT
http://blog.csdn.net/wlsfling/article/details/5876134http://www.lenky.info/archives/2012/03/1371http ...
- iOS-CGContextRef画各种图形例子
iOS-CGContextRef画各种图形例子 绘制效果图 绘制代码 - (void)drawRect:(CGRect)rect { //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你 ...
- Charles --- Mac 抓包工具
安装 官方网站Charles 是一款收费软件,可以免费体验30天.网上有破解版. 使用 infoq 上有一篇很棒的教程:iOS开发工具——网络封包分析工具Charles 注意事项 这是我使用过程中遇到 ...
- tcpdump 命令行抓包工具
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3898248.html ...
- $(this).next()与$(this).children()
$(this).next() 当前元素同级的下个元素,而非子元素 $(this).children() 是当前元素的下一级元素的集合,就是子元素的集合,而不管子元素的后代元素 所以这两个没有什么可比性 ...