結果

問題 No.886 Direct
ユーザー NyaanNyaanNyaanNyaan
提出日時 2019-09-28 17:16:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 523 ms / 4,000 ms
コード長 9,553 bytes
コンパイル時間 1,743 ms
コンパイル使用メモリ 170,828 KB
実行使用メモリ 38,536 KB
最終ジャッジ日時 2024-04-10 10:42:47
合計ジャッジ時間 7,551 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,948 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 4 ms
6,940 KB
testcase_23 AC 122 ms
19,716 KB
testcase_24 AC 150 ms
21,364 KB
testcase_25 AC 73 ms
14,532 KB
testcase_26 AC 102 ms
18,016 KB
testcase_27 AC 430 ms
36,000 KB
testcase_28 AC 383 ms
34,872 KB
testcase_29 AC 509 ms
38,348 KB
testcase_30 AC 523 ms
38,404 KB
testcase_31 AC 482 ms
38,512 KB
testcase_32 AC 378 ms
38,264 KB
testcase_33 AC 492 ms
38,536 KB
testcase_34 AC 471 ms
38,404 KB
testcase_35 AC 508 ms
38,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define whlie while
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define rep(i,N) for(int i = 0; i < (N); i++)
#define repr(i,N) for(int i = (N) - 1; i >= 0; i--)
#define rep1(i,N) for(int i = 1; i <= (N) ; i++)
#define repr1(i,N) for(int i = (N) ; i > 0 ; i--)
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define vrep(v,it) for(auto it = v.begin(); it != v.end(); it++)
#define vrepr(v,it) for(auto it = v.rbegin(); it != v.rend(); it++)
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
using namespace std; void solve();
using ll = long long; using vl = vector<ll>;
using vi = vector<int>; using vvi = vector< vector<int> >;
constexpr int inf = 1001001001;
constexpr ll infLL = (1LL << 61) - 1;
struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7);} } iosetupnya;
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; }
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#ifdef NyaanDebug
  #include "NyaanDebug.h"
  #define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0)
  #define trca(v,N) do { cerr << #v << " = "; array_out(v , N);cout << endl;} while(0)
#else
  #define trc(...)
  #define trca(...)
  int main(){solve();}
#endif

using P = pair<double,double>; using vp = vector<P>;
constexpr int MOD = /**/ 1000000007; //*/ 998244353;
//////////


struct divisor_transform{
  template <typename T>
  static constexpr void zeta_transform(vector<T> &a){
    int N = a.size() - 1; 
    vector<int> sieve(N + 1, true);
    for(int p = 2; p <= N; p++)
      if(sieve[p])
        for(int k = 1; k * p <= N; ++k)
          sieve[k * p] = false , a[k * p] += a[k];    
  }

  template<typename T>
  static constexpr void mobius_transform(T &a){
    int N = a.size() - 1; 
    vector<int> sieve(N + 1, true);
    for(int p = 2; p <= N; p++)
      if(sieve[p])
        for(int k = N / p; k > 0; --k)
          sieve[k * p] = false , a[k * p] -= a[k];    
  }
  
  // verify 
  // https://atcoder.jp/contests/arc064/submissions/7707249
  template<typename T>
  static constexpr void zeta_transform(map<long long, T> &a){
    for(auto &x : a) for(auto &y : a){
      if(x == y) break;
      if(x.first % y.first == 0) x.second += y.second;
    }
  }
  template<typename T>
  static constexpr void mobius_transform(map<long long, T> &a){
    for(auto &x : a) for(auto &y : a){
      if(x == y) break;
      if(x.first % y.first == 0) x.second -= y.second;
    }
  }

};

// verify
// https://atcoder.jp/contests/agc038/submissions/7683063
// https://www.codechef.com/viewsolution/26767783
struct multiple_transform{
  template <typename T>
  static constexpr void zeta_transform(vector<T> &a){
    int N = a.size() - 1;
    vector<int> sieve(N + 1, true);
    for(int p = 2; p <= N; ++p)
      if(sieve[p])
        for(int k = N / p; k > 0; --k)
          sieve[k * p] = false , a[k] += a[k * p];
  }
  template <typename T>
  static constexpr void mobius_transform(vector<T> &a){
    int N = a.size() - 1;
    vector<int> sieve(N + 1, true);
    for(int p = 2; p <= N; ++p)
      if(sieve[p])
        for(int k = 1; k * p <= N; ++k)
          sieve[k * p] = false , a[k] -= a[k * p];
  }

  template<typename T>
  static constexpr void zeta_transform(map<long long, T> &a){
    for(auto it=a.rbegin(); it!=a.rend(); it++)
      for(auto it2=a.rbegin(); it2!=it; it2++)
        if(it2->first % it->first == 0)
          it->second += it2->second;
  }
  template<typename T>
  static constexpr void mobius_transform(map<long long, T> &a){
    for(auto it=a.rbegin(); it!=a.rend(); it++)
      for(auto it2=a.rbegin(); it2!=it; it2++)
        if(it2->first % it->first == 0)
          it->second -= it2->second;
  }

};

template<typename T>
static constexpr vector<T> mobius_function(int N){
  vector<T> a(N + 1 , 0);
  a[1] = 1;
  divisor_transform::mobius_transform(a);
  return a;
}

template<int N>
struct constexpr_mobius_function{
  int mobius[N + 1] , sieve[N + 1];
  constexpr constexpr_mobius_function(): mobius() , sieve(){
    for(int i=1; i<=N; i++) sieve[i] = 1, mobius[i] = 0;
    mobius[1] = 1;
    for(int p = 2; p <= N; p++)
      if(sieve[p])
        for(int k = N / p; k > 0; --k)
          sieve[k * p] = false , mobius[k * p] -= mobius[k];    
  }
  const int& operator[](int i)const{return mobius[i];}
};

// N = 1000000 , pnum = 78498
template<int N,int pnum> struct constexpr_prime{
  int prime[pnum];
  int sieve[N + 1];
  constexpr_prime() : prime() , sieve() {
    for(int i=2;i<=N;i++) sieve[i]=1;
    int idx = 0;
    for(long long p = 2; p <= N; p++){
      if(sieve[p]){
        prime[idx++] = p;
        for(long long j = p * p; j <= N; j += p) sieve[j] = 0;
      }
    }
  }
  const long long& operator[](long long i) const{return prime[i];}
};

// verify
template<typename T,typename F>
static constexpr unordered_map<long long,T> divisor_zeta_transform(long long N, F f){
  // factorization
  unordered_map<long long,long long> factors;
  {
    long long M = N;
    for(long long i = 2; i * i <= M; i++)
      while(M % i == 0) factors[i]++ , M /= i;
    if(M != 1) factors[M]++;
  }
  unordered_map<long long,T> ret;
  ret.emplace(1 , 1);
  for(auto &d : factors){
    auto ret2 = ret;
    T prev = 1;
    for(long long i = 1 , cur = d.first; 
      i <= d.second;
      i++ , cur *= d.first){
      T val = ( prev += f(cur) );
      for(auto &x : ret) 
        ret2.emplace(x.first*cur , x.second*val);
    }
    swap(ret , ret2);
  }
  return ret;
}

// verify
// https://onlinejudge.u-aizu.ac.jp/status/users/NyaanNyaan/submissions/1/NTL_1_D/judge/3892694/C++14
// https://atcoder.jp/contests/abc020/submissions/7695313
template<typename T,typename F>
static constexpr unordered_map<long long,T> divisor_mobius_transform(long long N, F f){
  // factorization
  unordered_map<long long,long long> factors;
  {
    long long M = N;
    for(long long i = 2; i * i <= M; i++)
      while(M % i == 0) factors[i]++ , M /= i;
    if(M != 1) factors[M]++;
  }
  unordered_map<long long,T> ret;
  ret.emplace(1 , 1);
  for(auto &d : factors){
    auto ret2 = ret;
    for(long long i = 1,cur = d.first , prev = 1; 
      i <= d.second; 
      i++ , cur *= d.first , prev *= d.first){
      T val = f(cur) - f(prev);
      for(auto &x : ret) 
        ret2.emplace(x.first*cur , x.second*val);
    }
    swap(ret , ret2);
  }
  return ret;
}

template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< MOD >;

void solve(){
  inl(H,W);
  int N = max(H,W); 
  vector<modint> a(N + 1 , 0) , b(N + 1 , 0);
  modint ans = H * (W - 1) + W * (H - 1);
  rep1(i,H) a[i] = H - i;
  rep1(i,W) b[i] = W - i;
  multiple_transform::zeta_transform(a);
  multiple_transform::zeta_transform(b);
  rep1(i,N) a[i] *= b[i];
  multiple_transform::mobius_transform(a);
  out(ans + a[1] * 2);
  

}
0