結果

問題 No.1185 完全な3の倍数
ユーザー samagaisamagai
提出日時 2020-09-06 18:09:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,483 bytes
コンパイル時間 966 ms
コンパイル使用メモリ 99,540 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 14:12:33
合計ジャッジ時間 2,409 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,384 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <cassert>
#include <random>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef long long int ll;
const ll INF = 10000000000;
const double PI = acos(-1);
const ll mod = 998244353;

ll n = 0;
int d = 0;
int cnt = 0;

// 整数nの桁数を計算する関数
int CalcDigit(ll num) {
    int digit = 0;
    while (num != 0) {
        num /= 10;
        digit++;
    }
    return digit;
}

void dfs(ll num, int digit) {
    if (digit == d) {
        if (num <= n) cnt++;
        return;
    }
    int arr[4] = { 0,3,6,9 };
    rep(i, 4) {
        dfs(num * 10 + arr[i], digit+1);
    }
    return;
}

int main()
{
    cin >> n;

    d = CalcDigit(n);
    cnt = 0;
    // 2ケタの場合、数え上げ
    if (d <= 2) {
        for (int i = 10; i <= n; i++) {
            int a = i / 10;
            int b = i % 10;
            if (i % 3 == 0 && (a + b) % 3 == 0) {
                cnt++;
            }
        }
        cout << cnt << endl;
        return 0;
    }

    // 2ケタでない場合、まず、1つ下のケタまでを求める
    cnt += 30;  // 2ケタの完全な3の倍数の数
    for (int i = 3; i < d; i++) {
        cnt += pow(4, i - 1) * 3;
    }

    int arr[3] = { 3, 6, 9 };
    rep(i, 3) {
        dfs(arr[i], 1);
    }
    cout << cnt << endl;
    return 0;
}
0