poj 1416 Shredding Company 二进制枚举
来源:互联网 发布:数控车u型槽的编程 编辑:程序博客网 时间:2024/05/16 23:44
题意:
给两个数n和m,先要求把m切成几个分片,这几个分片之和为s,求max(s)<=n,若s不唯一,输出rejected,若s不存在,输出error,若s存在唯一,则按从左往右的顺序输出分片
例子:
n=50 m=12346
则s=1+2+34+6=43 并且是唯一的,所以输出43 1 2 34 6
n=12 m=111
则s=1+11=11+1=12 不唯一 输出rejected
若n=m 输出n n
若n=m=0 结束
代码:
//author: CHC//First Edit Time: 2015-06-25 14:18#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <set>#include <vector>#include <map>#include <queue>#include <set>#include <algorithm>#include <limits>using namespace std;typedef long long LL;int tx,numx,res[10],ans[10],tmp[10];void getlen(int numx){ res[0]=0; res[1]=0; if(numx==0){ res[0]=1; return ; } while(numx){ res[++res[0]]=numx%10; numx/=10; //printf("%d\n",res[res[0]]); }}int getnum(int l,int r){ int ans=0; for(int i=r;i>=l;i--){ ans=ans*10+res[i]; } return ans;}int checknum(int x){ tmp[0]=0; int tans=0; int l=0,r=0; while(1){ if(l==res[0])break; r=l+1; while((x&(1<<r))==0)++r; tmp[++tmp[0]]=getnum(l+1,r); tans+=tmp[tmp[0]]; l=r; } return tans;}void solve(){ if(tx==numx){ printf("%d %d\n",numx,numx); return ; } getlen(numx); int tt=(1<<(res[0]-1)); int cnt=0; int tmax=-1; for(int i=0;i<tt;i++){ int k=i; k=(k<<1)|1; k=k|(1<<res[0]); //i=(i<<1)|1; //i=i|(1<<(res[0])); int tans=checknum(k); if(tmax==-1){ if(tans<=tx){ ++cnt; tmax=tans; memcpy(ans,tmp,sizeof(tmp)); } } else if(tans<=tx&&tmax<=tans){ if(tmax==tans)++cnt; else cnt=1; tmax=tans; memcpy(ans,tmp,sizeof(tmp)); } } if(tmax==-1)puts("error"); else if(cnt>1)puts("rejected"); else { printf("%d",tmax); for(int i=ans[0];i>=1;i--){ printf(" %d",ans[i]); } puts(""); }}int main(){ while(~scanf("%d%d",&tx,&numx)&&(tx||numx))solve(); return 0;}
0 0
- poj 1416 Shredding Company 二进制枚举
- poj 1416 Shredding Company 模拟+枚举
- poj 1416 Shredding Company
- POJ 1416 Shredding Company
- POJ 1416 Shredding Company
- POJ-1416-Shredding Company
- POJ 1416 Shredding Company
- POJ 1416 Shredding Company
- poj 1416 Shredding Company
- POJ 1416 Shredding Company
- POJ 1416 Shredding Company
- POJ 1416 Shredding Company
- poj 1416 Shredding Company
- poj 1416 Shredding Company
- Shredding Company(POJ 1416)
- 【POJ 1416】 Shredding Company
- Shredding Company(POJ--1416
- POJ 1416:Shredding Company
- Android 2.3.3 SDK 源代码关联Eclipse ?
- Python使用__future__
- 回溯算法
- 用Python和Pygame写游戏-从入门到精通(14)
- 二叉树系统
- poj 1416 Shredding Company 二进制枚举
- Invert Binary Tree
- JVM -XX:NewRatio、-XX:SurvivorRatio参数含义
- 用Python和Pygame写游戏-从入门到精通(15)
- Tomcat启动时卡在“INFO: Deploying web application directory ......”的解决方法
- 用Python和Pygame写游戏-从入门到精通(16)
- BNUOJ 12887 isumi (计算几何+最小割)
- jquery 的datatable的属性说明
- 用Python和Pygame写游戏-从入门到精通(17)