結果
| 問題 |
No.434 占い
|
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2017-04-21 09:36:13 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 20 ms / 2,000 ms |
| コード長 | 1,119 bytes |
| コンパイル時間 | 1,747 ms |
| コンパイル使用メモリ | 170,032 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-20 05:00:05 |
| 合計ジャッジ時間 | 3,279 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
const int inv[9] = { 0, 1, 5, 0, 7, 2, 0, 4, 8 };
int modpow(int a, int n, int mod = 9) {
a %= mod;
if (a == 0) return 0;
int r = 1;
while (n) r = r*((n % 2) ? a : 1) % mod, a = a*a%mod, n >>= 1;
return r;
}
int sol(string S) {
vector<int> v;
for (char c : S) v.push_back(c - '0');
int sm = 0;
for (int i : v) sm += i;
if (sm == 0) return 0;
int a = 1, b = 1, t = 0;
int res = 0;
int n = v.size();
rep(i, 0, n) {
int c = a * inv[b] * modpow(3, t) % 9;
res = (res + v[i] * c) % 9;
a *= (n - 1 - i);
b *= (i + 1);
while (a % 3 == 0 && 0 < a) a /= 3, t++;
while (b % 3 == 0 && 0 < b) b /= 3, t--;
a %= 9; b %= 9;
}
if (res == 0) res = 9;
return res;
}
//-----------------------------------------------------------------------------------
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int T; cin >> T;
rep(t, 0, T) {
string s; cin >> s;
printf("%d\n", sol(s));
}
}
はまやんはまやん