結果

問題 No.187 中華風 (Hard)
ユーザー eQeeQe
提出日時 2024-09-27 03:41:42
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,446 bytes
コンパイル時間 5,827 ms
コンパイル使用メモリ 336,884 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-27 03:42:13
合計ジャッジ時間 8,708 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 175 ms
5,376 KB
testcase_03 AC 164 ms
5,376 KB
testcase_04 AC 173 ms
5,376 KB
testcase_05 AC 174 ms
5,376 KB
testcase_06 AC 173 ms
5,376 KB
testcase_07 AC 172 ms
5,376 KB
testcase_08 AC 96 ms
5,376 KB
testcase_09 AC 94 ms
5,376 KB
testcase_10 AC 106 ms
5,376 KB
testcase_11 AC 176 ms
5,376 KB
testcase_12 AC 175 ms
5,376 KB
testcase_13 AC 32 ms
5,376 KB
testcase_14 AC 37 ms
5,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 136 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 175 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
namespace my{
void main();
void solve();
}
int main(){my::main();}
namespace my{
#define eb emplace_back
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define VV(n,...) vec<ll>__VA_ARGS__;setsize({n},__VA_ARGS__);vin(__VA_ARGS__)
#define FO(n) for(ll ij=0;ij<n;++ij)
#define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))
#define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ulll=__uint128_t;
using lll=__int128_t;
istream&operator>>(istream&i,ulll&x){ull t;i>>t;x=t;return i;}
ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<<x/10)<<ll(x%10);}
istream&operator>>(istream&i,lll&x){ll t;i>>t;x=t;return i;}
ostream&operator<<(ostream&o,const lll&x){return o<<string(x<0,'-')<<ulll(x>0?x:-x);}
constexpr char nl=10;
constexpr char sp=32;
auto range(bool s,ll a,ll b=9e18,ll c=1){if(b==9e18)b=a,(s?b:a)=0;return tuple{a-s,b,c};}
ll rand(ll l=9e18,ll r=9e18){static ull a=495;a^=a<<7,a^=a>>9;return r!=9e18?a%(r-l)+l:l!=9e18?a%l:a;}
lll pw(lll x,lll n,ll m=0){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,m?r%=m,x%=m:r,n>>=1;return r;}
template<class...A>auto max(const A&...a){return max(initializer_list<common_type_t<A...>>{a...});}

template<class A,class B>struct pair{
  A a;B b;
  pair()=default;
  pair(A a,B b):a(a),b(b){}
  pair(std::pair<A,B>p):a(p.first),b(p.second){}
  bool operator==(const pair&p)const{return a==p.a&&b==p.b;}
  auto operator<=>(const pair&p)const{return a!=p.a?a<=>p.a:b<=>p.b;}
  friend ostream&operator<<(ostream&o,const pair&p){return o<<p.a<<sp<<p.b;}
};

template<class F=less<>>auto&sort(auto&a,F f={}){ranges::sort(a,f);return a;}

template<class T,class U>istream&operator>>(istream&i,pair<T,U>&p){return i>>p.first>>p.second;}
template<class T,class U>ostream&operator<<(ostream&o,const pair<T,U>&p){return o<<p.first<<sp<<p.second;}

template<class T,class U>ostream&operator<<(ostream&o,const unordered_map<T,U>&m){fe(m,e)o<<e.first<<sp<<e.second<<nl;return o;}
template<size_t n>ostream&operator<<(ostream&o,const bitset<n>&b){fo(i,n)o<<b[i];return o;}

template<class V>concept isv=is_base_of_v<vector<typename V::value_type>,V>;
template<class T>struct core_type{using type=T;};
template<isv V>struct core_type<V>{using type=typename core_type<typename V::value_type>::type;};
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>ostream&operator<<(ostream&o,const vector<V>&v){fe(v,e)o<<e<<string(&e!=&v.back(),isv<V>?nl:sp);return o;}

template<class V>struct vec:vector<V>{
  using vector<V>::vector;
  vec(const vector<V>&v){fe(v,e)this->eb(e);}
  template<size_t n>vec(const bitset<n>&a){fo(i,n)this->eb(a[i]);}

  vec&operator+=(const vec&u){vec&v=*this;fo(i,v.size())v[i]+=u[i];return v;}
  vec&operator-=(const vec&u){vec&v=*this;fo(i,v.size())v[i]-=u[i];return v;}
  vec&operator^=(const vec&u){fe(u,e)this->eb(e);return*this;}
  vec operator+(const vec&u)const{return vec(*this)+=u;}
  vec operator-(const vec&u)const{return vec(*this)-=u;}
  vec operator^(const vec&u)const{return vec(*this)^=u;}
  vec&operator++(){fe(*this,e)++e;return*this;}
  vec&operator--(){fe(*this,e)--e;return*this;}
  vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;}

  auto scan(auto f)const{pair<typename core_type<V>::type,bool>r{};fe(*this,e)if constexpr(!isv<V>)r.b?f(r.a,e),r:r={e,1};else if(auto s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;return r;}
  auto max()const{return scan([](auto&a,const auto&b){a<b?a=b:0;}).a;}
};
template<class T=ll,size_t n,size_t i=0>auto make_vec(const ll(&s)[n],T x={}){if constexpr(n==i+1)return vec<T>(s[i],x);else{auto X=make_vec<T,n,i+1>(s,x);return vec<decltype(X)>(s[i],X);}}
template<ll n,class...A>void setsize(const ll(&l)[n],A&...a){((a= make_vec(l,typename core_type<A>::type())),...);}

void io(){cin.tie(0)->sync_with_stdio(0);cout<<fixed<<setprecision(15);cerr<<nl;}
void lin(auto&...a){(cin>>...>>a);}
void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);}
template<char c=sp>void pp(const auto&...a){ll n=sizeof...(a);((cout<<a<<string(--n>0,c)),...);cout<<nl;}

template<class T,class U=T>auto rle(const vec<T>&a){vec<pair<T,U>>r;fe(a,e)r.size()&&e==r.back().a?++r.back().b:r.eb(e,1).b;return r;}
template<class T,class U=T>auto rce(vec<T>a){return rle<T,U>(sort(a));}

namespace sgt{
template<class T>T max(T a,T b){return b<a?a:b;}
}

struct montgomery64{
  using i64=ll;
  using u64=ull;
  using u128=__uint128_t;

  static inline u64 m=998244353;
  static inline u64 miv;
  static inline u64 n2;

  static void set_mod(u64 m){
    assert(m<(1ULL<<63));
    assert(m&1);
    montgomery64::m=m;
    n2=-u128(m)%m;
    miv=m;
    fo(5)miv*=2-m*miv;
    assert(m*miv==1);
  }

  static u64 mod(){
    return m;
  }

  u64 a;
  montgomery64(const i64&a=0):a(reduce((u128)(a%(i64)m+m)*n2)){}

  static u64 reduce(const u128&a){
    u128 r=(a+u128(u64(a)*-miv)*m)>>64;
    return r>=m?r-m:r;
  }

  auto&operator+=(const montgomery64&b){if((a+=b.a)>=m)a-=m;return*this;}
  auto&operator-=(const montgomery64&b){if(i64(a-=b.a)<0)a+=m;return*this;}
  auto&operator*=(const montgomery64&b){a=reduce(u128(a)*b.a);return*this;}
  auto&operator/=(const montgomery64&b){*this*=b.inv();return*this;}

  auto operator+(const montgomery64&b)const{return montgomery64(*this)+=b;}
  auto operator-(const montgomery64&b)const{return montgomery64(*this)-=b;}
  auto operator*(const montgomery64&b)const{return montgomery64(*this)*=b;}
  auto operator/(const montgomery64&b)const{return montgomery64(*this)/=b;}
  bool operator==(const montgomery64&b)const{return a==b.a;}
  bool operator!=(const montgomery64&b)const{return a!=b.a;}
  auto operator-()const{return montgomery64()-montgomery64(*this);}

  montgomery64 pow(u128 n)const{
    montgomery64 r(1),x(*this);
    while(n){
      if(n&1)r*=x;
      x*=x;
      n>>=1;
    }
    return r;
  }

  montgomery64 inv()const{
    u64 a=this->a,b=m,u=1,v=0;
    while(b)u-=a/b*v,swap(u,v),a-=a/b*b,swap(a,b);
    return u;
  }

  u64 val()const{
    return reduce(a);
  }

  friend istream&operator>>(istream&i,montgomery64&b){
    ll t;i>>t;b=t;
    return i;
  }

  friend ostream&operator<<(ostream&o,const montgomery64&b){
    return o<<b.val();
  }
};

template<class modint>bool miller_rabin(ll n,vec<ll>as){
  ll d=n-1;
  while(~d&1)d>>=1;

  if(modint::mod()!=n)modint::set_mod(n);
  modint one=1,minus_one=n-1;
  fe(as,a){
    if(a%n==0)continue;
    ll t=d;
    modint y=modint(a).pow(t);
    while(t!=n-1&&y!=one&&y!=minus_one)y*=y,t<<=1;
    if(y!=minus_one&&~t&1)return 0;
  }
  return 1;
}

bool is_prime(ll n){
  if(~n&1)return n==2;
  if(n<=1)return 0;
  if(n<4759123141LL)return miller_rabin<montgomery64>(n,{2,7,61});
  return miller_rabin<montgomery64>(n,{2,325,9375,28178,450775,9780504,1795265022});
}

template<class modint>ll pollard_rho(ll n){
  if(~n&1)return 2;
  if(is_prime(n))return n;
  if(modint::mod()!=n)modint::set_mod(n);
  modint R,one=1;
  auto f=[&](const modint&x){return x*x+R;};
  while(1){
    modint x,y,ys,q=one;
    R=rand(2,n),y=rand(2,n);
    ll g=1;
    constexpr ll m=128;
    for(ll r=1;g==1;r<<=1){
      x=y;
      fo(r)y=f(y);
      for(ll k=0;g==1&&k<r;k+=m){
        ys=y;
        for(ll i=0;i<m&&i<r-k;++i)q*=x-(y=f(y));
        g=std::gcd(q.val(),n);
      }
    }
    if(g==n)do g=std::gcd((x-(ys=f(ys))).val(),n);while(g==1);
    if(g!=n)return g;
  }
}

vec<ll>inner_factorize(ll n){
  if(n<=1)return{};
  ll d=pollard_rho<montgomery64>(n);
  if(d==n)return{d};
  return inner_factorize(d)^inner_factorize(n/d);
}

vec<pair<ll,ll>>factorize(ll n){
  return rce(inner_factorize(n));
}

template<class T>T mod(T a,T m){a%=m;return a<0?a+m:a;}

template<class T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class...A>auto gcd(const A&...a){common_type_t<A...>r=0;((r=gcd(r,a)),...);return r;}
template<class...A>auto lcm(const A&...a){common_type_t<A...>r=1;((r=lcm(r,a)),...);return r;}

template<class T>pair<T,T>ax_by_g(T a,T b){
  if(b==0)return{1,0};
  auto[s,t]=ax_by_g(b,a%b);
  return{t,s-a/b*t};
}

ll inv_mod(ll a,ll m){
  assert(gcd(a,m)==1);
  auto[x,y]=ax_by_g(a,m);
  return mod(x,m);
}

template<class T>T chinese_remainder_theorem_coprime(const vec<T>&a,const vec<T>&m,T M=-1){
  ll K=a.size();
  vec<T>t(K),S(K),P(K,1);
  T SM=0,PM=1;
  fo(i,K){
    t[i]=mod((a[i]-S[i])*inv_mod(P[i],m[i]),m[i]);
    fo(j,i+1,K){
      (S[j]+=t[i]*P[j]%m[j])%=m[j];
      (P[j]*=m[i])%=m[j];
    }

    if(M==-1){
      SM+=t[i]*PM;
      PM*=m[i];
    }else{
      (SM+=t[i]*PM%M)%=M;
      (PM*=m[i])%=M;
    }
  }
  return SM;
}

template<class T>T chinese_remainder_theorem(const vec<T>&a,const vec<T>&m,T M=-1){
  ll K=a.size();
  fo(i,K)fo(j,i+1,K)if((a[i]-a[j])%gcd(m[i],m[j]))return-1;

  unordered_map<T,pair<T,T>>mem;
  fo(i,K){
    auto primes=factorize(m[i]);
    fe(primes,p,b)if(!mem.count(p)||mem[p].b<b)mem[p]={a[i],b};
  }

  vec<T>pa,pm;
  fe(mem,p,v){
    T mod=pw(p,v.b);
    pa.eb(v.a%mod);
    pm.eb(mod);
  }
  return chinese_remainder_theorem_coprime(pa,pm,M);
}

void main(){io();ll T=1;fo(T)solve();}
void solve(){
  LL(N);
  VV(N,a,m);
  ll M=atcoder::modint1000000007::mod();
  ll LCM=1;
  fo(i,N)LCM=lcm(LCM,m[i])%M;
  pp(a.max()==0?LCM:chinese_remainder_theorem(a,m,M));
}}
0