Codeforces 626C. Block Towers
2016-02-14 20:16
716 查看
C. Block Towers
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.
The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.
Input
The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.
Output
Print a single integer, denoting the minimum possible height of the tallest tower.
Examples
input
output
input
output
input
output
Note
In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.
In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
思路:分别给你的是p个2和q个3,那么这个要求的最小的数T肯定>=max(2*p,3*q);那么从max(2*q,3*q)开始,暴力循环判断当前数是否符合要求。
当数为K的时候,K/2是2的倍数,K/3是3的倍数,K/6是6的倍数,其中,6的倍数是必须要符合的,因为6的倍数是由2,或3得到的,那么(K/2-K/6)是2的倍数且不能除3;
同理(K/3-K/6)是3的倍数但不能整除2;x=(p-(K/2-K/6))就是能去补给6的个数,结果<0的话就x=0;这表示2的个数连那些单独只能被2整除的数都补齐,所以给6的就为0,当
然可以存在2的个数连那些单独只能被2整除的数都补齐的情况,就像p=0的情况(可以这样是因为2,3是独立的),同理x1=(p-(K/3-K/6));如果x1+x==K/6,那么就是K;
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.
The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.
Input
The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.
Output
Print a single integer, denoting the minimum possible height of the tallest tower.
Examples
input
1 3
output
9
input
3 2
output
8
input
5 0
output
10
Note
In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.
In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
思路:分别给你的是p个2和q个3,那么这个要求的最小的数T肯定>=max(2*p,3*q);那么从max(2*q,3*q)开始,暴力循环判断当前数是否符合要求。
当数为K的时候,K/2是2的倍数,K/3是3的倍数,K/6是6的倍数,其中,6的倍数是必须要符合的,因为6的倍数是由2,或3得到的,那么(K/2-K/6)是2的倍数且不能除3;
同理(K/3-K/6)是3的倍数但不能整除2;x=(p-(K/2-K/6))就是能去补给6的个数,结果<0的话就x=0;这表示2的个数连那些单独只能被2整除的数都补齐,所以给6的就为0,当
然可以存在2的个数连那些单独只能被2整除的数都补齐的情况,就像p=0的情况(可以这样是因为2,3是独立的),同理x1=(p-(K/3-K/6));如果x1+x==K/6,那么就是K;
#include<set> #include<stack> #include<queue> #include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<map> using namespace std; int main(void) { int i,j,k,p,q; while(scanf("%d %d",&p,&q)!=EOF) { int cc=2*p; int dd=3*q; int yy=max(cc,dd); int flag=0; for(i=yy;; i++) { if(i%2==0||i%3==0) { int uu=i/2; int kk=i/3; int zz=i/6; int x1=(uu-zz); int x2=(kk-zz); x1=p-x1; x2=q-x2; if(x1<0) { x1=0; } if(x2<0) { x2=0; }; if(x1+x2==zz) { break; } } } printf("%d\n",i); } }
#include <iostream> #include <cstdio> #include <cstring> int er(int n,int m,int a,int b); using namespace std; int main() { int a,b; scanf("%d%d",&a,&b); int r=10000000; int k=er(0,r,a,b); printf("%d\n",k); return 0; } int er(int n,int m,int a,int b) { int mid=(n+m)/2; if(n>m) { return n; } int cnt=max(a-mid/2+mid/6,0)+max(b-mid/3+mid/6,0); if(mid/6>=cnt) { return er(n,mid-1,a,b); } else return er(mid+1,m,a,b); }
while(l<=r) { int mid=(l+r)>>1; int ret=max(a-mid/2+mid/6,0)+max(b-mid/3+mid/6,0); if(mid/6==ret) { break; }else if(mid/6>ret) { r=mid-1; } else l=mid+1; }
相关文章推荐
- 图的存储
- Shell expr的用法
- ps commad
- Matlab randperm 洗牌函数
- uva11100 - The Trip, 2007
- Spring组件扫描<context:component-scan/>使用详解
- BZOJ3173 TJOI2013最长上升子序列(Treap+ZKW线段树)
- 狂赚310亿美元!安卓系统的钱究竟是怎么来的
- BZOJ3173 TJOI2013最长上升子序列(Treap+ZKW线段树)
- Linux中开发c和c++的IDE的选择
- KeeperException$UnimplementedException
- Unity开发中总结的一些小技巧
- 阶段小项目2:显示bin格式图片
- 推荐几款实用的Android Studio 插件
- 内存管理
- 图论概述
- 逻辑代数原理及其应用
- 最新版Butterknife plugin支持butterknife7.0.1和兼容butterknife 6.1.0及以下
- BZOJ3172 TJOI2013 单词
- scrollTo和scrollBy