結果

問題 No.1185 完全な3の倍数
ユーザー tnakao0123
提出日時 2020-08-23 16:58:09
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,154 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 94,244 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-24 09:34:24
合計ジャッジ時間 1,972 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1185.cc:  No.1185 完全な3の倍数 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_K = 10;

/* typedef */

/* global variables */

int ds[MAX_K], es[MAX_K];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  n++;

  int c2 = 0;
  for (int i = 1; i <= 9; i++)
    for (int j = 0; j <= 9; j++)
      if ((i + j) % 3 == 0) {
	if (i * 10 + j >= n) {
	  printf("%d\n", c2);
	  return 0;
	}
	c2++;
      }
  //printf("c2=%d\n", c2);

  int k = 0;
  for (int m = n; m > 0; m /= 10) ds[k++] = m % 10;
  //printf("k=%d\n", k);

  es[0] = 1;
  for (int i = 1; i < k; i++) es[i] = es[i - 1] * 4;

  int sum = 0;
  for (int i = k - 1; i >= 0; i--) {
    sum += ((ds[i] + 2) / 3) * es[i];
    if (ds[i] % 3) break;
  }

  sum += c2 - es[2];
  printf("%d\n", sum);
  return 0;
}
0