結果

問題 No.1185 完全な3の倍数
ユーザー akaneakane
提出日時 2020-08-22 15:48:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,711 bytes
コンパイル時間 2,338 ms
コンパイル使用メモリ 201,068 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-16 00:26:59
合計ジャッジ時間 3,911 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 31 ms
4,384 KB
testcase_04 AC 35 ms
4,384 KB
testcase_05 AC 33 ms
4,384 KB
testcase_06 AC 29 ms
4,376 KB
testcase_07 AC 30 ms
4,380 KB
testcase_08 AC 34 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,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 10 ms
4,380 KB
testcase_20 AC 25 ms
4,380 KB
testcase_21 AC 18 ms
4,380 KB
testcase_22 AC 12 ms
4,376 KB
testcase_23 AC 6 ms
4,380 KB
testcase_24 AC 25 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 17 ms
4,380 KB
testcase_27 AC 7 ms
4,380 KB
testcase_28 AC 19 ms
4,380 KB
testcase_29 AC 14 ms
4,380 KB
testcase_30 AC 16 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 31 ms
4,376 KB
testcase_33 AC 4 ms
4,380 KB
testcase_34 AC 25 ms
4,376 KB
testcase_35 AC 33 ms
4,384 KB
testcase_36 AC 26 ms
4,380 KB
testcase_37 AC 25 ms
4,380 KB
testcase_38 AC 16 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i < (n); i++)
using namespace std;
using ll = long long;

int ctoi(char x){
    return x - '0';
}

int solve1(int N, int ans){
    for(int i=111; i<=N; i+=3){
        string s = to_string(i);
        bool flg=true;
        for(int j=0; j<s.size(); j++){
            if(ctoi(s[j])%3 != 0){
                flg=false;
                break;
            }
        }
        if(flg) ans++;
    }
    return ans;
}

int solve2(int N, int ans){
    int i=300;
    while(i<=N){
        // if(i<1000) cerr << i << " ";
        string s = to_string(i);
        bool flg=true;
        for(int j=0; j<s.size(); j++){
            if(ctoi(s[j])%3 != 0){
                flg=false;
                break;
            }
        }
        if(flg) ans++;
        int ret=3;
        int t = s.size();
        // if(i<1000) cerr << s[t-1] << endl;
        bool flg2 = false;
        int ct = 0;
        while(t-1>=0){
            if(s[t-1]=='9'){
                ret*= 10;
                ct++;
                flg2 = true;
            }else{
                break;
            }
            t--;
        }
        i += ret;
        if(flg2){
            int y = 9;
            ct--;
            while(ct--){
                y = y + y*10;
            }
            i -= y;
        }

    }
    return ans;

}


int main(){
    ios::sync_with_stdio(false);
    int N; cin>>N;

    ll ans = 0;
    ll t=10;
    // 2桁の特殊処理
    while(t<100){
        int a = t%10;
        int b = (t/10)%t;
        if((a+b)%3==0){
            ans ++;
        }
        if(t==N) break;
        t++;
    }

    // cout << solve1(N,ans) << endl;
    cout << solve2(N,ans) << endl;

}
0