結果

問題 No.432 占い(Easy)
ユーザー @abcde@abcde
提出日時 2019-02-22 20:15:50
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,179 bytes
コンパイル時間 1,697 ms
コンパイル使用メモリ 145,824 KB
実行使用メモリ 11,512 KB
最終ジャッジ日時 2023-08-16 12:13:06
合計ジャッジ時間 3,323 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,340 KB
testcase_01 AC 6 ms
11,204 KB
testcase_02 AC 6 ms
11,208 KB
testcase_03 AC 6 ms
11,440 KB
testcase_04 WA -
testcase_05 AC 6 ms
11,324 KB
testcase_06 AC 6 ms
11,328 KB
testcase_07 WA -
testcase_08 AC 6 ms
11,148 KB
testcase_09 AC 8 ms
11,212 KB
testcase_10 AC 7 ms
11,208 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 6 ms
11,200 KB
testcase_16 AC 7 ms
11,156 KB
testcase_17 AC 6 ms
11,164 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 7 ms
11,160 KB
testcase_25 AC 8 ms
11,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int LIMIT = 1e3;

// 各桁の和を, 1 ~ 9 で返却.
// @param N: 正整数.
// @return ret: 各桁の和(1 ~ 9).
LL digitSum(LL N){
    LL ret = N;
    while(ret >= 10){
        LL q = ret / 10;
        LL r = ret % 10;
        ret = q + r;
    }
    return ret;
}

int main() {
    
    // 1. 入力情報取得.
    int T;
    cin >> T;
    
    // 2. 桁数和 を 変換.
    // 2-1. nCr ついて, 桁数和の形で保存.
    // -> 25 × 12 = 300 と (2 + 5) × (1 + 2) = 21 
    // -> どちらも, 桁数和は, 3 (※3 + 0 + 0 と 2 + 1 であるため)
    // -> 2数の積と, 各桁の和に直した後の2数の積は, どちらも各桁の和が等しくなる性質があるらしい.
    LL nCr[LIMIT][LIMIT + 1];
    for(int i = 0; i < LIMIT; i++) nCr[i][0] = 1;
    for(int j = 1; j < LIMIT + 1; j++) nCr[0][j] = 0;
    for(int i = 1; i < LIMIT; i++) for(int j = 1; j < LIMIT + 1; j++) nCr[i][j] = digitSum(nCr[i - 1][j] + nCr[i - 1][j - 1]);
    
    // for(int i = 0; i < 9; i++){
    //     for(int j = 0; j < 9; j++){
    //         cout << nCr[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    
    // 2. 数字占いする.
    for(int i = 0; i < T; i++){
        string S;
        cin >> S;
        LL l = S.size();
        // 2-1. 文字列の長さ 1 なら終了し, 次へ.
        if(l == 1){
            cout << S << endl;
            continue;
        }
        
        // 2-2. 文字列の長さ 2以上 の場合は?
        // パスカルの三角形を考える???
        LL ans = 0;
        LL n = digitSum(l - 1);  // nCr の n の index
        // r が 0 ~ l - 1 の 場合.
        // cout << "ans=" << ans << " l=" << l << " n=" << n << endl;
        for(int i = 0; i < l; i++){
            LL r = digitSum(i);  // nCr の r の index
            ans += (S[i] - '0') * nCr[n][r];
            // cout << (S[i] - '0') << " " << nCr[n][r] << " r=" << r << endl;
            ans = digitSum(ans);
            // cout << "ans=" << ans << endl;
        }
        cout << ans << endl;
    }
    
    // 3. 終了.
    return 0;
    
}
0