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, ...
随机推荐
- 触发器记录表某一个字段数据变化的日志 包括插入insert 修改update 删除delete 操作
本文参考:http://www.cnblogs.com/lyhabc/articles/3236985.html ,), ), ), ), ...
- [PWA] Caching with Progressive libraries
Mainly introduce two libaraies: sw-precache and sw-toolbox. Install: npm install --save-dev sw-preca ...
- javascript中涉及到汉字的比较
在使用js中的"=="进行字符串的比较时,发现在英文情况下是ok的,但在中文比较时则不行了. 在网上搜索,提供了一个解决方法,使用 stringObject.localeCompa ...
- java获取计算机硬件参数
public class HardWareUtils { /** * * 获取主板序列号 * * * * @return */ public static String g ...
- 使用附加导航(affix)实现内容切换
<!DOCTYPE html> <html> <head> <title> new document </title> <meta c ...
- Bash远程文件传输命令scp
备份远程文件(远程——>本地) scp -r 远程用户名@ip:文件绝对路径 本地绝对路径 还原远程文件(本地——>远程) scp -r 本地路径 远程用户名@ip:远程绝对路径 如果SS ...
- Weex 学习教程
一.环境搭建 1.安装Node,官网下载(http://nodejs.org/) 2.查看npm安装版本,终端输入:npm -v版本不能低于2.15.1 3.安装weex-toolkit,终端输入:n ...
- JQ 日期格式化
将字符转换为日期格式: function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$ ...
- 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)
转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...
- (whh仅供自己参考)进行ip网络请求的步骤
这个过程大致是这个样子: 1 添加通知 2 发送网络请求 里边有一个发送通知的操作 3 执行发送通知的具体操作 代码如下: 1 在VC添加通知 [[NSNotificationCenter defau ...