結果
問題 | No.167 N^M mod 10 |
ユーザー | soraie_ |
提出日時 | 2021-03-23 11:23:02 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 49,159 bytes |
コンパイル時間 | 6,186 ms |
コンパイル使用メモリ | 297,060 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-25 03:09:33 |
合計ジャッジ時間 | 8,542 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 2 ms
6,820 KB |
testcase_07 | AC | 1 ms
6,816 KB |
testcase_08 | AC | 2 ms
6,820 KB |
testcase_09 | AC | 2 ms
6,816 KB |
testcase_10 | AC | 2 ms
6,820 KB |
testcase_11 | AC | 2 ms
6,816 KB |
testcase_12 | AC | 2 ms
6,816 KB |
testcase_13 | AC | 2 ms
6,820 KB |
testcase_14 | AC | 2 ms
6,820 KB |
testcase_15 | AC | 2 ms
6,816 KB |
testcase_16 | AC | 2 ms
6,816 KB |
testcase_17 | AC | 2 ms
6,820 KB |
testcase_18 | AC | 2 ms
6,816 KB |
testcase_19 | AC | 2 ms
6,816 KB |
testcase_20 | AC | 3 ms
6,820 KB |
testcase_21 | AC | 2 ms
6,816 KB |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
ソースコード
#ifdef _DEBUG #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; //-------------------------------------------------------------------- #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define overload4(_1,_2,_3,_4,name,...) name #define rep1(n) for(ll _=0;_<(ll)n;++_) #define rep2(i,n) for(ll i=0;i<(ll)n;++i) #define rep3(i,a,b) for(ll i=(ll)a;i<(ll)b;++i) #define rep4(i,a,b,c) for(ll i=(ll)a;i<(ll)b;i+=(ll)c) #define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__) #ifdef _DEBUG #define pass(...) __VA_ARGS__ ; #define debug1(a) cerr<<#a<<": "<<a<<"\n" #define debug2(a,b) cerr<<#a<<": "<<a<<", "<<#b<<": "<<b<<"\n" #define debug3(a,b,c) cerr<<#a<<": "<<a<<", "<<#b<<": "<<b<<", "<<#c<<": "<<c<<"\n" #define debug4(a,b,c,d) cerr<<#a<<": "<<a<<", "<<#b<<": "<<b<<", "<<#c<<": "<<c<<", "<<#d<<": "<<d<<"\n" /* #define debug1(a) cout<<#a<<": "<<a<<"\n" #define debug2(a,b) cout<<#a<<": "<<a<<", "<<#b<<": "<<b<<"\n" #define debug3(a,b,c) cout<<#a<<": "<<a<<", "<<#b<<": "<<b<<", "<<#c<<": "<<c<<"\n" #define debug4(a,b,c,d) cout<<#a<<": "<<a<<", "<<#b<<": "<<b<<", "<<#c<<": "<<c<<", "<<#d<<": "<<d<<"\n" */ #define debug(...) overload4(__VA_ARGS__,debug4,debug3,debug2,debug1)(__VA_ARGS__) #define koko cerr << "koko\n"; #else #define debug(...) void(0) #define pass(...) void(0); #define koko void(0); #endif #define mp make_pair //#define fi first //#define se second void myset(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);} void doset(int n){cout << fixed << setprecision(n);cerr << fixed << setprecision(n);} using ll = long long; using ld = long double; using dou = double; template<class First,class Second>ostream& operator<<(ostream& os,const pair<First,Second>& pp) {return os << "{" << pp.first << "," << pp.second << "}";} template<class T>ostream& operator<<(ostream& os,const vector<T>& VV) {if(VV.empty())return os<<"[]";os<<"[";rep(i,VV.size())os<<VV[i]<<(i==int(VV.size()-1)?"]":",");return os;} template<class T>ostream& operator<<(ostream& os,const set<T>& SS) {if(SS.empty())return os<<"[]";os<<"[";auto ii=SS.begin();for(;ii!=SS.end();ii++)os<<*ii<<(ii==prev(SS.end())?"]":",");return os;} template<class Key,class Tp>ostream& operator<<(ostream& os,const map<Key,Tp>& MM) {if(MM.empty())return os<<"[]";os<<"[";auto ii=MM.begin();for(;ii!=MM.end();ii++)os<<"{"<<ii->first<<":"<<ii->second<<"}"<<(ii==prev(MM.end())?"]":",");return os;} const int inf = 1 << 30; const ll INF = 1LL << 61; const ld pi = 3.14159265358; const ll mod1 = 1000000007LL; const ll mod2 = 998244353LL; typedef pair<ll,ll> P; template<class T, class U> inline bool chmin(T& a, const U& b){ if(a > b){ a = b; return 1; } return 0; } template<class T, class U> inline bool chmax(T& a, const U& b){ if(a < b){ a = b; return 1; } return 0; } ll modpow(ll n,ll m,ll MOD){ if(m == 0)return 1; if(m < 0)return 0; ll res = 1; n %= MOD; while(m){ if(m & 1)res = (res * n) % MOD; m >>= 1; n *= n; n %= MOD; } return res; } ll mypow(ll n,ll m){ if(m == 0)return 1; if(m < 0)return -1; ll res = 1; while(m){ if(m & 1)res = (res * n); m >>= 1; n *= n; } return res; } inline bool isp(ll n){ bool res = true; if(n == 1 || n == 0)return false; else{ for(ll i = 2;i * i <= n;i++){ if(n % i == 0){ res = false; break; } } return res; } } inline bool Yes(bool b = 1){cout << (b ? "Yes\n":"No\n");return b;} inline bool YES(bool b = 1){cout << (b ? "YES\n":"NO\n");return b;} map<ll,ll> primefactor(ll n){ map<ll,ll> ma; if(n <= 1)return ma; ll m = n; for(ll i = 2;i * i <= n;i++){ while(m % i == 0){ ma[i]++; m /= i; } } if(m != 1)ma[m]++; return ma; } vector<ll> divisor(ll n,bool sorted = true,bool samein = false){ vector<ll> res; for(ll i = 1;i * i <= n;i++){ if(n % i == 0){ res.push_back(i); if(i * i != n || samein)res.push_back(n / i); } } if(sorted)sort(all(res)); return res; } ll __lcm(ll a,ll b){return a / __gcd(a,b) * b;} template<class T>T sum(const vector<T> &V){T r=0;for(auto x:V)r+=x;return r;} template<class T>T sum(const initializer_list<T> &V){T r=0;for(auto x:V)r+=x;return r;} //#include <atcoder/all> //#include "atcoder/lazysegtree.hpp" //using namespace atcoder; //-------------------------------------------------------------------- long long extgcd(long long a,long long b,long long &x,long long &y){ if(b == 0){ x = 1; y = 0; return a; } long long d = extgcd(b,a % b,y,x); y -= a / b * x; return d; } long long modinv(long long a,long long p){ long long b = p, u = 1, v = 0; while(b){ long long t = a / b; a -= t * b;std::swap(a, b); u -= t * v;std::swap(u, v); } u %= p; if(u < 0)u += p; return u; } /** * @brief garner precalc * @return long long */ long long pregarner(std::vector<long long> &rems,std::vector<long long> &mods,long long MOD){ assert(rems.size() == mods.size()); for(int i = 0;i < int(rems.size());i++){ for(int j = 0;j < i;j++){ long long g = std::gcd(mods[i],mods[j]); if((rems[i] - rems[j]) % g != 0)return -1; mods[i] /= g;mods[j] /= g; long long gi = std::gcd(g,mods[i]),gj = g / gi; do{ g = std::gcd(gi,gj); gi *= g;gj /= g; }while(g != 1); mods[i] *= gi;mods[j] *= gj; rems[i] %= mods[i];rems[j] %= mods[j]; } } long long res = 1; for(long long &a : mods)res = res * a % MOD; return res; } /** * @brief returns a integer where rems[i](mod.mods[i]). * @return long long */ long long garner(std::vector<long long> rems,std::vector<long long> mods,long long MOD){ assert(rems.size() == mods.size()); mods.push_back(MOD); int sz = mods.size(); std::vector<long long> cof(sz,1LL),con(sz,0LL); for(int i = 0;i < sz - 1;i++){ long long cur = (rems[i] - con[i]) * modinv(cof[i],mods[i]) % mods[i]; if(cur < 0)cur += mods[i]; for(int j = i + 1;j < sz;j++){ con[j] = (con[j] + cur * cof[j]) % mods[j]; cof[j] = cof[j] * mods[i] % mods[j]; } } return con.back(); } template<long long MOD = 1000000007> struct modint { using mint = modint<MOD>; long long x; modint(long long a = 0):x((a % MOD + MOD) % MOD){} inline constexpr modint operator-()const noexcept{return modint(-x);} inline constexpr modint &operator+=(const modint &a)noexcept{ if ((x += a.x) >= MOD) x -= MOD; return *this; } inline constexpr modint &operator-=(const modint &a)noexcept{ if ((x -= a.x) < 0) x += MOD; return *this; } inline constexpr modint &operator*=(const modint &a)noexcept{ (x *= a.x) %= MOD; return *this; } inline constexpr modint &operator++()noexcept{ x++; if(x == MOD)x = 0; return *this; } inline constexpr modint operator++(int)noexcept{ modint res(*this); operator++(); return res; } inline constexpr modint &operator--()noexcept{ x--; if(x == -1)x = MOD - 1; return *this; } inline constexpr modint operator--(int)noexcept{ modint res(*this); operator--(); return res; } inline constexpr modint operator+(const modint &a)const noexcept{ modint res(*this); return res += a; } inline constexpr modint operator-(const modint &a)const noexcept{ modint res(*this); return res -= a; } inline constexpr modint operator*(const modint &a)const noexcept{ modint res(*this); return res *= a; } inline constexpr modint inv()const{ long long a = x,b = MOD,u = 1,v = 0; while(b){ long long t = a / b; a -= t * b;std::swap(a,b); u -= t * v;std::swap(u,v); } return u; } inline constexpr modint &operator/=(const modint &a)noexcept{return (*this) *= a.inv();} inline constexpr modint operator/(const modint &a)const noexcept{ modint res(*this); return res /= a; } inline constexpr bool operator==(const modint &a)const noexcept{return x == a.x;} friend std::istream &operator>>(std::istream &is,modint &a) { is >> a.x; a.x = (a.x % MOD + MOD) % MOD; return is; } friend std::ostream &operator<<(std::ostream &os,const modint &a){ os << a.x; return os; } long long getmod(){return MOD;} friend mint modpow(mint a,long long b)noexcept{ mint res(1); while(b){ if(b & 1)res *= a; a *= a; b >>= 1; } return res; } }; //using mint = modint<1'000'000'007>::mint; //ex. (2013265921,137,27),(998244353,31,23),(469762049,30,26) template<long long MOD,int base,int max_exp> struct NTT_primitive{ using mint = modint<MOD>; std::vector<mint> bases,invs; NTT_primitive(){ bases.resize(max_exp + 1);invs.resize(max_exp + 1); bases[max_exp] = base; invs[max_exp] = mint(base).inv(); for(int i = max_exp - 1;i >= 0;i--){ bases[i] = bases[i + 1] * bases[i + 1]; invs[i] = invs[i + 1] * invs[i + 1]; } } void dft(std::vector<mint>& vec,int t){ int sz = vec.size(); if(sz == 1)return; std::vector<mint> veca,vecb; for(int i = 0;i < sz / 2;i++){ veca.push_back(vec[i * 2]); vecb.push_back(vec[i * 2 + 1]); } dft(veca,t); dft(vecb,t); int e = __builtin_ffsll(sz) - 1; mint now = 1,zeta = (t == 1 ? bases[e]:invs[e]); for(int i = 0;i < sz;i++){ vec[i] = veca[i % (sz / 2)] + now * vecb[i % (sz / 2)]; now *= zeta; } } std::vector<mint> convolution(const std::vector<mint>& A,const std::vector<mint>& B){ // assert(A.size() == B.size()); int sz = 1; while(sz < int(A.size() + B.size()))sz <<= 1; std::vector<mint> f(sz),g(sz); for(int i = 0;i < int(A.size());i++)f[i] = A[i]; for(int i = 0;i < int(B.size());i++)g[i] = B[i]; dft(f,1);dft(g,1); for(int i = 0;i < sz;i++)f[i] = f[i] * g[i]; dft(f,-1); mint inv = mint(sz).inv(); for(int i = 0;i < sz;i++)f[i] *= inv; return f; } void dft(std::vector<long long>& vec,int t){ int sz = vec.size(); if(sz == 1)return; std::vector<long long> veca,vecb; for(int i = 0;i < sz / 2;i++){ veca.push_back(vec[i * 2]); vecb.push_back(vec[i * 2 + 1]); } dft(veca,t); dft(vecb,t); int e = __builtin_ffsll(sz) - 1; long long now = 1,zeta = (t == 1 ? bases[e].x:invs[e].x); for(int i = 0;i < sz;i++){ vec[i] = (veca[i % (sz / 2)] + now * vecb[i % (sz / 2)] % MOD) % MOD; now = now * zeta % MOD; } } std::vector<long long> convolution(const std::vector<long long>& A,const std::vector<long long>& B){ // assert(A.size() == B.size()); int sz = 1; while(sz < int(A.size() + B.size()))sz <<= 1; std::vector<long long> f(sz),g(sz); for(int i = 0;i < int(A.size());i++)f[i] = A[i] % MOD; for(int i = 0;i < int(B.size());i++)g[i] = B[i] % MOD; dft(f,1);dft(g,1); for(int i = 0;i < sz;i++)f[i] = f[i] * g[i] % MOD; dft(f,-1); long long inv = modinv(sz,MOD); for(int i = 0;i < sz;i++)f[i] = f[i] * inv % MOD; return f; } }; template<long long MOD> struct NTT_all{ NTT_primitive<2013265921,137,27> ntt1; NTT_primitive<998244353,31,23> ntt2; NTT_primitive<469762049,30,26> ntt3; using mint = modint<MOD>; using mint1 = modint<2013265921>; using mint2 = modint<998244353>; using mint3 = modint<469762049>; NTT_all(){} std::vector<mint> convolution(const std::vector<mint>& A,const std::vector<mint>& B){ int sza = A.size(),szb = B.size(); std::vector<mint1> A1(sza),B1(szb); std::vector<mint2> A2(sza),B2(szb); std::vector<mint3> A3(sza),B3(szb); for(int i = 0;i < sza;i++){ A1[i] = A[i].x;A2[i] = A[i].x;A3[i] = A[i].x; } for(int i = 0;i < szb;i++){ B1[i] = B[i].x;B2[i] = B[i].x;B3[i] = B[i].x; } auto C1 = ntt1.convolution(A1,B1); auto C2 = ntt2.convolution(A2,B2); auto C3 = ntt3.convolution(A3,B3); int rs = C1.size(); std::vector<mint> res(rs); for(int i = 0;i < rs;i++){ std::vector<long long> r = {C1[i].x,C2[i].x,C3[i].x},m = {2013265921,998244353,469762049}; res[i] = garner(r,m,MOD); } return res; } std::vector<long long> convolution(const std::vector<long long>& A,const std::vector<long long>& B){ int sza = A.size(),szb = B.size(); std::vector<long long> A1(sza),B1(szb),A2(sza),B2(szb),A3(sza),B3(szb); for(int i = 0;i < sza;i++){ A1[i] = A[i] % 2013265921; A2[i] = A[i] % 998244353; A3[i] = A[i] % 469762049; } for(int i = 0;i < szb;i++){ B1[i] = B[i] % 2013265921; B2[i] = B[i] % 998244353; B3[i] = B[i] % 469762049; } auto C1 = ntt1.convolution(A1,B1); auto C2 = ntt2.convolution(A2,B2); auto C3 = ntt3.convolution(A3,B3); int rs = C1.size(); std::vector<long long> res(rs); for(int i = 0;i < rs;i++){ std::vector<long long> r = {C1[i],C2[i],C3[i]},m = {2013265921,998244353,469762049}; res[i] = garner(r,m,MOD) % MOD; } return res; } }; namespace bigint_convolution{ std::function<std::vector<long long>(std::vector<long long>,std::vector<long long>)> convolution; void set(const std::function<std::vector<long long>(std::vector<long long>,std::vector<long long>)>& f){convolution = f;} std::vector<long long> naive(const std::vector<long long>& a,const std::vector<long long>& b){ int n = int(a.size()),m = int(b.size()); std::vector<long long> res(n + m - 1); if(n < m) for(int j = 0;j < m;j++)for(int i = 0;i < n;i++)res[i + j] += a[i] * b[j]; else for(int i = 0;i < n;i++)for(int j = 0;j < m;j++)res[i + j] += a[i] * b[j]; return res; }; }; template<int d,int base> struct bigint{ using bint = bigint<d,base>; bigint(const std::string& _s){ sign = 1; dat.clear(); if(_s.empty())return; if(_s[0] == '-')sign = -1; std::string s = sign == 1 ? _s : _s.substr(1); int i; for(i = int(s.size()) - d;i >= 0;i -= d){ assert('0' <= s[i] && s[i] <= '9'); dat.push_back(std::stoll(s.substr(i,d))); } if(i + d)dat.push_back(std::stoll(s.substr(0,i + d))); while(!dat.empty() && dat.back() == 0)dat.pop_back(); } bigint(long long n = 0){ sign = 1; dat.clear(); if(n < 0)sign = -1,n = -n; while(n){ dat.push_back(n % base); n /= base; } } int size()const{return int(dat.size());} int number_len()const{ if(size() == 0)return 0; int res = (int(dat.size()) - 1) * d; int b = 1,p = 0; while(b <= dat.back())p++,b *= 10; return res + p; } std::string to_string()const{ if(size() == 0)return "0"; std::string res = ""; if(sign == -1)res = "-"; auto to_string_pad = [](long long m,int _d){ std::string r = std::to_string(m); std::string ap(_d - int(r.size()),'0'); return ap + r; }; for(int i = size() - 1;i >= 0;i--){ if(i != size() - 1)res += to_string_pad(dat[i],d); else res += std::to_string(dat[i]); } return res; } long long to_ll()const{ long long res = 0; for(long long x : dat)res = res * base + x; return sign * res; } void norm(){ if(dat.empty())return; for(int i = 0;i < size() - 1;i++){ long long nex = dat[i] / base; if(nex * base > dat[i])nex--; dat[i] -= nex * base; dat[i + 1] += nex; } while(dat.back() >= base){ long long nex = dat.back() / base; dat.back() -= nex * base; dat.push_back(nex); } while(!dat.empty() && dat.back() == 0)dat.pop_back(); } inline constexpr bool operator<(const bint& a)const noexcept{ if(a.dat.empty())return false; else if(sign != a.sign)return sign < a.sign; else if(sign == -1)return (-a) < (-bint(*this)); else if(number_len() != a.number_len())return number_len() < a.number_len(); else{ for(int i = size() - 1;i >= 0;i--)if(dat[i] != a.dat[i])return dat[i] < a.dat[i]; return false; } } inline constexpr bool operator>(const bint& a)const noexcept{return a < bint(*this);} inline constexpr bool operator<=(const bint& a)const noexcept{return !(a < bint(*this));} inline constexpr bool operator>=(const bint& a)const noexcept{return !(bint(*this) < a);} inline constexpr bool operator==(const bint& a)const noexcept{return dat == a.dat;} inline constexpr bool operator!=(const bint& a)const noexcept{return dat != a.dat;} inline constexpr bint operator-()const noexcept{ bint res(*this); res.sign = -res.sign; return res; } friend bint abs(const bint& a){return a.sign == -1 ? -a : a;} inline constexpr bint &operator+=(const bint& a)noexcept{ if(sign != a.sign)return *this -= (-a); else{ if(size() < a.size())dat.resize(a.size()); for(int i = 0;i < a.size();i++)dat[i] += a.dat[i]; norm(); return *this; } } inline constexpr bint &operator-=(const bint& a)noexcept{ if(sign != a.sign)return *this += (-a); else if(abs(bint(*this)) < abs(a)){ *this = a - *this;sign = -sign; return *this; } else{ for(int i = 0;i < a.size();i++)dat[i] -= a.dat[i]; norm(); return *this; } } inline constexpr bint &operator++()noexcept{return *this += bint(1);} inline constexpr bint operator++(int)noexcept{operator++();return bint(*this);} inline constexpr bint &operator--()noexcept{return *this -= bint(1);} inline constexpr bint operator--(int)noexcept{operator--();return bint(*this);} inline constexpr bint &operator*=(const bint& a)noexcept{ if(size() == 0 || a.size() == 0)return *this = bint(0); if(std::min(size(),a.size()) <= 60)dat = bigint_convolution::naive(dat,a.dat); else dat = bigint_convolution::convolution(dat,a.dat); norm(); sign *= a.sign; return *this; } inline constexpr bint mul2()const noexcept{ bint res(*this); for(auto& x : res.dat)x *= 2; res.norm(); return res; } inline constexpr bint div2()const noexcept{ bint res(*this); int nex = 0; for(int i = res.size() - 1;i >= 0;i--){ long long cur = (res.dat[i] + nex * base); res.dat[i] = cur >> 1; nex = cur & 1; } while(!res.dat.empty() && res.dat.back() == 0)res.dat.pop_back(); return res; } inline constexpr int rem2()const noexcept{ if(dat.empty())return 0; else return dat[0] & 1; } inline constexpr bint &operator>>=(int k)noexcept{ dat = std::vector<long long>(dat.begin() + std::min(k,int(size())),dat.end()); return *this; } inline constexpr bint &operator<<=(int k)noexcept{ if(!dat.empty()){ std::vector<long long> add(k,0); dat.insert(dat.begin(),add.begin(),add.end()); } return *this; } inline constexpr bint &operator/=(long long v)noexcept{ if(v < 0)sign = -sign,v = -v; for(int i = size() - 1,rem = 0;i >= 0;i--){ long long cur = dat[i] + rem * (long long)(base); dat[i] = (long long)(cur / v); rem = (long long)(cur % v); } norm(); return *this; } friend std::pair<bint,bint> divmod_naive(const bint& _a,const bint& _b){ bint zero,s,t; bint a = abs(_a),b = abs(_b); if(a < b)return std::make_pair(zero,a); bint ar = b; s.dat.resize(a.size()),t.dat.resize(b.size()); int tx = a.size() - 1; for(;tx >= 0;--tx)if(a.dat[tx] > 0)break; for(int i = tx; i >= 0; --i){ t <<= 1; t += a.dat[i]; long long lo = 0,hi = base; if(t >= ar){ while(hi - lo > 1){ int mid = (hi + lo) / 2; if(ar * mid > t)hi = mid; else lo = mid; } t -= ar * lo; } s.dat[i] = lo; } if(_a.sign == _b.sign)s.sign = 1,t.sign = 1; else s.sign = -1,t.sign = 1; s.norm();t.norm(); return std::make_pair(s,t); } inline constexpr bint &operator/=(const bint& a)noexcept{ if(size() - a.size() <= 10)return *this = divmod_naive(bint(*this),a).first; else return *this = this->div_fast(a); } inline constexpr bint &operator%=(const bint& a)noexcept{ if(size() - a.size() <= 10)return *this = divmod_naive(bint(*this),a).second; else return *this = bint(*this) - this->div_fast(a) * a; } inline constexpr bint operator+(const bint& a)const noexcept{return bint(*this) += a;} inline constexpr bint operator-(const bint& a)const noexcept{return bint(*this) -= a;} inline constexpr bint operator*(const bint& a)const noexcept{return bint(*this) *= a;} inline constexpr bint operator>>(int k)const noexcept{return bint(*this) >>= k;} inline constexpr bint operator<<(int k)const noexcept{return bint(*this) <<= k;} inline constexpr bint operator/(long long v)const noexcept{return bint(*this) /= v;} inline constexpr bint operator/(const bint& a)const noexcept{return bint(*this) /= a;} inline constexpr bint operator%(const bint& a)const noexcept{return bint(*this) %= a;} friend std::ostream &operator<<(std::ostream& os,const bint& a){return os << a.to_string();} friend std::istream &operator>>(std::istream& is,bint& a){ std::string s; is >> s; a = bint(s); return is; } explicit operator bool()const noexcept{return !dat.empty();} explicit operator int()const noexcept{return to_ll();} using long_long = long long; explicit operator long_long()const noexcept{return to_ll();} int sign; std::vector<long long> dat; bint div_fast(const bint& _a)const noexcept{ bint a = abs(*this),b = abs(_a); if(a < b)return bint(); int tar = a.size() - b.size() + 1,m = 1; while(b.dat.back() * m * 10 < base)m *= 10; bint inv(m * base),pre(0); const bint two(2); while(inv != pre){ pre = inv; inv *= (two << 2) - inv * b.dat.back(); inv >>= 2; } int cur = 2,bcur = 1; pre = bint(0); while(inv != pre){ bint c; c.dat = std::vector<long long>(b.dat.end() - bcur,b.dat.end()); pre = inv; inv *= (two << (cur + bcur - 1)) - inv * c; int nex = std::min(cur << 1,tar); inv.dat = std::vector<long long>(inv.dat.end() - nex,inv.dat.end()); cur = nex; bcur = std::min(bcur << 1,b.size()); } inv.dat = std::vector<long long>(inv.dat.end() - tar,inv.dat.end()); bint res = a * inv;res.dat = std::vector<long long>(res.dat.begin() + a.size(),res.dat.end()); bint mul = res * b; while(mul + b <= a){ res++; mul += b; } res.sign = sign * _a.sign; return res; } }; using bint = bigint<4,10000>; #include <type_traits> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder #ifdef _MSC_VER #include <intrin.h> #endif #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { // @param m `1 <= m` // @return x mod m constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } // Fast modular multiplication by barrett reduction // Reference: https://en.wikipedia.org/wiki/Barrett_reduction // NOTE: reconsider after Ice Lake struct barrett { unsigned int _m; unsigned long long im; // @param m `1 <= m < 2^31` explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} // @return m unsigned int umod() const { return _m; } // @param a `0 <= a < m` // @param b `0 <= b < m` // @return `a * b % m` unsigned int mul(unsigned int a, unsigned int b) const { // [1] m = 1 // a = b = im = 0, so okay // [2] m >= 2 // im = ceil(2^64 / m) // -> im * m = 2^64 + r (0 <= r < m) // let z = a*b = c*m + d (0 <= c, d < m) // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2 // ((ab * im) >> 64) == c or c + 1 unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; // @param n `0 <= n` // @param m `1 <= m` // @return `(x ** n) % m` constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } // Reference: // M. Forisek and J. Jancina, // Fast Primality Testing for Integers That Fit into a Machine Word // @param n `0 <= n` constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; constexpr long long bases[3] = {2, 7, 61}; for (long long a : bases) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template <int n> constexpr bool is_prime = is_prime_constexpr(n); // @param b `1 <= b` // @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; // Contracts: // [1] s - m0 * a = 0 (mod b) // [2] t - m1 * a = 0 (mod b) // [3] s * |m1| + t * |m0| <= b long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b // [3]: // (s - t * u) * |m1| + t * |m0 - m1 * u| // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u) // = s * |m1| + t * |m0| <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } // by [3]: |m0| <= b/g // by g != b: |m0| < b/g if (m0 < 0) m0 += b / s; return {s, m0}; } // Compile time primitive root // @param m must be prime // @return primitive root (and minimum in now) constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template <int m> constexpr int primitive_root = primitive_root_constexpr(m); // @param n `n < 2^32` // @param m `1 <= m < 2^32` // @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64) unsigned long long floor_sum_unsigned(unsigned long long n, unsigned long long m, unsigned long long a, unsigned long long b) { unsigned long long ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } unsigned long long y_max = a * n + b; if (y_max < m) break; // y_max < m * (n + 1) // floor(y_max / m) <= n n = (unsigned long long)(y_max / m); b = (unsigned long long)(y_max % m); std::swap(m, a); } return ans; } } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template <class T> using is_modint = std::is_base_of<modint_base, T>; template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>; } // namespace internal template <int m, std::enable_if_t<(1 <= m)>* = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint& operator*=(const mint& rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime<m>; }; template <int id> struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator*=(const mint& rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template <int id> internal::barrett dynamic_modint<id>::bt(998244353); using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template <class T> using is_static_modint = std::is_base_of<internal::static_modint_base, T>; template <class T> using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>; template <class> struct is_dynamic_modint : public std::false_type {}; template <int id> struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {}; template <class T> using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>; } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { template <class mint, internal::is_static_modint_t<mint>* = nullptr> void butterfly(std::vector<mint>& a) { static constexpr int g = internal::primitive_root<mint::mod()>; int n = int(a.size()); int h = internal::ceil_pow2(n); static bool first = true; static mint sum_e[30]; // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i] if (first) { first = false; mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1 int cnt2 = bsf(mint::mod() - 1); mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv(); for (int i = cnt2; i >= 2; i--) { // e^(2^i) == 1 es[i - 2] = e; ies[i - 2] = ie; e *= e; ie *= ie; } mint now = 1; for (int i = 0; i <= cnt2 - 2; i++) { sum_e[i] = es[i] * now; now *= ies[i]; } } for (int ph = 1; ph <= h; ph++) { int w = 1 << (ph - 1), p = 1 << (h - ph); mint now = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p] * now; a[i + offset] = l + r; a[i + offset + p] = l - r; } now *= sum_e[bsf(~(unsigned int)(s))]; } } } template <class mint, internal::is_static_modint_t<mint>* = nullptr> void butterfly_inv(std::vector<mint>& a) { static constexpr int g = internal::primitive_root<mint::mod()>; int n = int(a.size()); int h = internal::ceil_pow2(n); static bool first = true; static mint sum_ie[30]; // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i] if (first) { first = false; mint es[30], ies[30]; // es[i]^(2^(2+i)) == 1 int cnt2 = bsf(mint::mod() - 1); mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv(); for (int i = cnt2; i >= 2; i--) { // e^(2^i) == 1 es[i - 2] = e; ies[i - 2] = ie; e *= e; ie *= ie; } mint now = 1; for (int i = 0; i <= cnt2 - 2; i++) { sum_ie[i] = ies[i] * now; now *= es[i]; } } for (int ph = h; ph >= 1; ph--) { int w = 1 << (ph - 1), p = 1 << (h - ph); mint inow = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * inow.val(); } inow *= sum_ie[bsf(~(unsigned int)(s))]; } } } template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution_naive(const std::vector<mint>& a, const std::vector<mint>& b) { int n = int(a.size()), m = int(b.size()); std::vector<mint> ans(n + m - 1); if (n < m) { for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { ans[i + j] += a[i] * b[j]; } } } else { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; } } } return ans; } template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution_fft(std::vector<mint> a, std::vector<mint> b) { int n = int(a.size()), m = int(b.size()); int z = 1 << internal::ceil_pow2(n + m - 1); a.resize(z); internal::butterfly(a); b.resize(z); internal::butterfly(b); for (int i = 0; i < z; i++) { a[i] *= b[i]; } internal::butterfly_inv(a); a.resize(n + m - 1); mint iz = mint(z).inv(); for (int i = 0; i < n + m - 1; i++) a[i] *= iz; return std::move(a); } } // namespace internal template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution(std::vector<mint>&& a, std::vector<mint>&& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) return convolution_naive(a, b); return internal::convolution_fft(a, b); } template <class mint, internal::is_static_modint_t<mint>* = nullptr> std::vector<mint> convolution(const std::vector<mint>& a, const std::vector<mint>& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) return convolution_naive(a, b); return internal::convolution_fft(a, b); } template <unsigned int mod = 998244353, class T, std::enable_if_t<internal::is_integral<T>::value>* = nullptr> std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; using mint = static_modint<mod>; std::vector<mint> a2(n), b2(m); for (int i = 0; i < n; i++) { a2[i] = mint(a[i]); } for (int i = 0; i < m; i++) { b2[i] = mint(b[i]); } auto c2 = convolution(move(a2), move(b2)); std::vector<T> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { c[i] = c2[i].val(); } return c; } std::vector<long long> convolution_ll(const std::vector<long long>& a, const std::vector<long long>& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; static constexpr unsigned long long MOD1 = 754974721; // 2^24 static constexpr unsigned long long MOD2 = 167772161; // 2^25 static constexpr unsigned long long MOD3 = 469762049; // 2^26 static constexpr unsigned long long M2M3 = MOD2 * MOD3; static constexpr unsigned long long M1M3 = MOD1 * MOD3; static constexpr unsigned long long M1M2 = MOD1 * MOD2; static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3; static constexpr unsigned long long i1 = internal::inv_gcd(MOD2 * MOD3, MOD1).second; static constexpr unsigned long long i2 = internal::inv_gcd(MOD1 * MOD3, MOD2).second; static constexpr unsigned long long i3 = internal::inv_gcd(MOD1 * MOD2, MOD3).second; auto c1 = convolution<MOD1>(a, b); auto c2 = convolution<MOD2>(a, b); auto c3 = convolution<MOD3>(a, b); std::vector<long long> c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { unsigned long long x = 0; x += (c1[i] * i1) % MOD1 * M2M3; x += (c2[i] * i2) % MOD2 * M1M3; x += (c3[i] * i3) % MOD3 * M1M2; // B = 2^63, -B <= x, r(real value) < B // (x, x - M, x - 2M, or x - 3M) = r (mod 2B) // r = c1[i] (mod MOD1) // focus on MOD1 // r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B) // r = x, // x - M' + (0 or 2B), // x - 2M' + (0, 2B or 4B), // x - 3M' + (0, 2B, 4B or 6B) (without mod!) // (r - x) = 0, (0) // - M' + (0 or 2B), (1) // -2M' + (0 or 2B or 4B), (2) // -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1) // we checked that // ((1) mod MOD1) mod 5 = 2 // ((2) mod MOD1) mod 5 = 3 // ((3) mod MOD1) mod 5 = 4 long long diff = c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1)); if (diff < 0) diff += MOD1; static constexpr unsigned long long offset[5] = { 0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3}; x -= offset[diff % 5]; c[i] = x; } return c; } } // namespace atcoder bint modpow(bint a,bint b,bint c){ a %= c; bint res(1); while(b){ if(b.rem2())res = res * a % c; a = a * a % c; b = b.div2(); } return res; } int main(){ myset(); bint n,m; cin >> n >> m; cout << modpow(n,m,10) << "\n"; }