[ARC139] A - Trailing Zeros

[Q] https://atcoder.jp/contests/arc139/tasks/arc139_a

1. 末尾の0の数を確保したいので、まず右にbitシフトして逃がす。
2. 単調増加にしたいので、1加算する。
3. ずらしたのに0bit目が0だったら、0が多すぎちゃう。もう1加算して、0bit目を立てる。
4. 左にbitシフトして0を復元。

実装

int main(){
 cincout();
 
 ll N;
 cin >> N;
 ll now = 0;
 rep(i, N){
   ll a;
   cin >> a;
   now >>= a;
   now += 1;
   if (now%2 == 0) ++now;
   now <<= a;
 }
 cout << now << endl;
}

https://atcoder.jp/contests/arc139/submissions/31236819

いいなと思ったら応援しよう!