CodeForces 626C
Block Towers
time limit: per test
2 seconds
memory limit: per test
256 megabytes
input:
standard input
output:
standard output
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. 제목은 높이가 2인 벽돌을 사용할 수 있는 아이가 n명, 높이가 3인 벽돌을 사용할 수 있는 아이가 m명 있다. 그들은 자신을 위해 높이가 h인 탑을 만들 것이다. 그러나 아이들의 탑 높이는 같을 수 없다. 지금 너에게 n과 m을 주겠다. 최소 높이가 얼마냐고 묻는다.문제풀이 사고방식: 숫자 i 앞부분(i 포함)은 2 또는 3의 배수인 개수는 i/2+i/3-i/6개이고 가장 작은 탑은 max(2*n, 3*m)이며 최대 탑은 3000000(300000에서 2 또는 3배수는 1500000+100000000-500000=2000000)이기 때문에 이 두 개 수 사이에서 조건에 맞는 최소한의 탑 높이를 찾아내면 된다.
조건: 1.n+m는 반드시 n+m개가 2 또는 3의 배수인 것을 보증한다.h/2>=n은 전 h개수에 n개 2의 배수가 있음을 보증해야 한다.h/3>=m는 앞의 h개수가 m개3의 배수인 2000000도 크지 않고 직접적으로 폭력적일 수 있지만 2000000000을 주면 GG가 되기 때문에 이분법을 사용한다.
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int l=max(2*n,3*m),r=3000005;
while(l<r)
{
int y=(l+r)/2;
if(y/2+y/3-y/6>=n+m&&y/2>=n&&y/3>=m)r=y;
else l=y+1;
}
printf("%d
",l);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.