結果
| 問題 |
No.434 占い
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-19 18:31:16 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,897 bytes |
| コンパイル時間 | 1,949 ms |
| コンパイル使用メモリ | 179,300 KB |
| 実行使用メモリ | 10,496 KB |
| 最終ジャッジ日時 | 2024-11-22 16:53:25 |
| 合計ジャッジ時間 | 11,823 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 25 TLE * 2 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
string nstr(){
static const int MAX_LEN = 1000000;
static char res_[MAX_LEN];
scanf("%s",res_);
return string(res_);
}
template<class Val>
map<Val, int> factorize(Val n){
map<Val, int> res;
for(Val i=2; (long long)i*i<=n; ++i){
while(n%i==0){
n /= i;
++res[i];
}
}
if(n!=1) ++res[n];
return res;
}
int powMod(int n, int k, int mod){
if(n==0)return 0;
ll res = 1 % mod, nn = n;
while(k){
if(k&1)res=res*nn%mod;
nn=nn*nn%mod;
k>>=1;
}
return (int)res;
}
string S;
void solve() {
int N = sz(S), ans = 0;
if(count(all(S), '0') == N) {
puts("0");
return;
}
map<int, int> c;
rep(i, N) {
if(i != 0) {
map<int, int> a = factorize<>(N - i), b = factorize<>(i);
each(p, a)c[p.first] += p.second;
vi r;
each(p, b) {
c[p.first] -= p.second;
if(c[p.first] == 0) {
r.push_back(p.first);
}
}
each(x, r)c.erase(x);
}
int x = 1;
each(p, c)(x *= powMod(p.first, p.second, 9))%=9;
(ans += x * (S[i]-'0')) %= 9;
}
if(ans == 0)ans = 9;
printf("%d\n", ans);
}
int main(){
int T; cin >> T;
while(T--) {
S = nstr();
solve();
}
}