結果

問題 No.434 占い
ユーザー miyo2580miyo2580
提出日時 2024-02-08 23:29:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 2,552 bytes
コンパイル時間 3,459 ms
コンパイル使用メモリ 216,060 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2024-02-08 23:30:04
合計ジャッジ時間 6,413 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 3 ms
6,676 KB
testcase_08 AC 15 ms
6,676 KB
testcase_09 AC 13 ms
6,676 KB
testcase_10 AC 12 ms
6,676 KB
testcase_11 AC 10 ms
6,676 KB
testcase_12 AC 4 ms
6,676 KB
testcase_13 AC 14 ms
6,676 KB
testcase_14 AC 14 ms
6,676 KB
testcase_15 AC 142 ms
13,568 KB
testcase_16 AC 130 ms
6,676 KB
testcase_17 AC 118 ms
6,676 KB
testcase_18 AC 104 ms
6,676 KB
testcase_19 AC 87 ms
6,676 KB
testcase_20 AC 22 ms
6,676 KB
testcase_21 AC 142 ms
13,568 KB
testcase_22 AC 153 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 130 ms
6,676 KB
testcase_25 AC 142 ms
13,568 KB
testcase_26 AC 65 ms
7,424 KB
testcase_27 AC 111 ms
6,676 KB
testcase_28 AC 109 ms
6,676 KB
testcase_29 AC 87 ms
6,676 KB
testcase_30 AC 37 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (ll i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
using Graph = vector<vector<ll>>;
const long long INF = 1LL<<60;
const long long MOD = 1000000007;

// Sieve of Eratosthenes
// https://youtu.be/UTVg7wzMWQc?t=2774
struct Sieve {
  int n;
  vector<int> f, primes;
  Sieve(int n=1):n(n), f(n+1) {
    f[0] = f[1] = -1;
    for (ll i = 2; i <= n; ++i) {
      if (f[i]) continue;
      primes.push_back(i);
      f[i] = i;
      for (ll j = i*i; j <= n; j += i) {
        if (!f[j]) f[j] = i;
      }
    }
  }
  bool isPrime(int x) { return f[x] == x;}
  vector<int> factorList(int x) {
    vector<int> res;
    while (x != 1) {
      res.push_back(f[x]);
      x /= f[x];
    }
    return res;
  }
  vector<P> factor(int x) {
    vector<int> fl = factorList(x);
    if (fl.size() == 0) return {};
    vector<P> res(1, P(fl[0], 0));
    for (int p : fl) {
      if (res.back().first == p) {
        res.back().second++;
      } else {
        res.emplace_back(p, 1);
      }
    }
    return res;
  }
  vector<pair<ll,int>> factor(ll x) {
    vector<pair<ll,int>> res;
    for (int p : primes) {
      int y = 0;
      while (x%p == 0) x /= p, ++y;
      if (y != 0) res.emplace_back(p,y);
    }
    if (x != 1) res.emplace_back(x,1);
    return res;
  }
}sieve(350);

ll modpow(ll a, ll b, ll mod) {
	ll res = 1;
	while (b) {
		if (b & 1)res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}

int main()
{  
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll T;cin>>T;
    rep(ti,T){
      string s;cin>>s;
      bool flg=1;
      ll n=s.size();
      ll ans=0;
      Graph g(n,vec(9));
      repd(i,1,n){
        vector<pair<ll,int>> v=sieve.factor(i);
        for(auto[x,y]:v){
          g[i][x%9]+=y;
        }
      }
      rep(i,9){
        rep(j,n-1)g[j+1][i]+=g[j][i];
      }
      repd(i,0,n){
        if(s[i]!='0')flg=0;
        ll now=1;
        rep(j,9){
          ll x=g[n-1][j]-g[n-1-i][j];
          ll y=g[i][j];
          x-=y;
          x=modpow(j,x,9);
          now*=x;
          now%=9;
        }
        now*=s[i]-'0';
        ans+=now;
      }
      ans%=9;
      if(flg==0&&ans==0)ans=9;
      cout<<ans<<'\n';
    }
    return 0;
}
0