문제 링크
https://atcoder.jp/contests/abc420/tasks/abc420_a
알고리즘
수학
시간복잡도
$O(1)$
풀이
$X + Y$가 $12$보다 작거나 같으면 $X + Y$월이 되므로 쉽게 문제를 해결할 수 있습니다.
하지만, $X + Y \geq 13$인 순간부터 다시 $1$월이 되므로 $1$, $13$, $25$, $\dots$이 같은 $1$월이 되어야 하므로 $12p + q$꼴의 값은 $q$월이 됨을 알 수 있습니다. 이를 정리하면 $(X + Y - 1) \% 12 + 1$월이 정답임을 알 수 있습니다.
소스코드
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string.h>
#include <map>
#include <queue>
#include <unordered_map>
using namespace std;
typedef long long ll;
ll a[200005];
int main()
{
ll t = 1;
while(t--)
{
ll x,y;
scanf("%lld %lld",&x,&y);
printf("%lld",(x+y-1)%12+1);
}
}
Comments