結果

問題 No.434 占い
ユーザー tottoripapertottoripaper
提出日時 2016-10-19 18:03:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 1,908 bytes
コンパイル時間 1,894 ms
コンパイル使用メモリ 159,616 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-02 05:42:39
合計ジャッジ時間 2,865 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 17 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 19 ms
5,376 KB
testcase_20 AC 138 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 25 ms
5,376 KB
testcase_30 AC 111 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:45:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   45 |     scanf("%d", &T);
      |     ~~~~~^~~~~~~~~~
main.cpp:48:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |         scanf("%s", S);
      |         ~~~~~^~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

char S[100100];
int inv[100100];

template <typename T>
T expt(T a, T n, T mod = std::numeric_limits<T>::max()){
    T res = 1;
    while(n){
        if(n & 1){res = res * a % mod;}
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main(){
    //*
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    /*/
      /*/

    for(int i=1;i<=100000;++i){
        // generate inv
        if(i == 1 || i % 3 == 0){inv[i] = 1;}
        else{
            inv[i] = expt(i % 9, 5, 9);
        }
    }
    
    
    int T;
    scanf("%d", &T);

    for(int _=0;_<T;++_){
        scanf("%s", S);

        int l = strlen(S), n = l - 1;

        if(count(S, S+l, '0') == l){
            std::cout << 0 << std::endl;
            continue;
        }
        
        int three_n = 0, m = 1;

        int res = 0;
        for(int i=0;i<=n;++i){
            if(i >= 1){
                int x = i;
                while(x % 3 == 0){
                    --three_n;
                    x /= 3;
                }

                m = m * inv[x] % 9;
                
                x = n - i + 1;

                while(x % 3 == 0){
                    ++three_n;
                    x /= 3;
                }

                m = m * x % 9;
            }
            
            int a = S[i] - '0', three_n2 = three_n + (a % 3 == 0) + (a % 9 == 0);
            if(three_n2 < 2){
                res = (res + a * m % 9 * expt(3, three_n) % 9) % 9;
            }

            // std::cout << a << ", " << three_n2 << ", " << res << std::endl;
        }

        std::cout << (res > 0 ? res : 9) << std::endl;
    }
}
0