#include #include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define FOR(i,n) for(int i = 0; i < (n); i++) #define sz(c) ((int)(c).size()) #define ten(x) ((int)1e##x) #define all(v) (v).begin(), (v).end() using namespace std; using ll=long long; using P = pair; const long double PI=acos(-1); const ll INF=1e18; const int inf=1e9; template struct Fp{ ll val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } static constexpr int getmod() { return MOD; } constexpr Fp operator - () const noexcept { return val ? MOD - val : 0; } constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; } constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; } constexpr Fp& operator += (const Fp& r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp& operator -= (const Fp& r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp& operator *= (const Fp& r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp& operator /= (const Fp& r) noexcept { ll a = r.val, b = MOD, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b, swap(a, b); u -= t * v, swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator == (const Fp& r) const noexcept { return this->val == r.val; } constexpr bool operator != (const Fp& r) const noexcept { return this->val != r.val; } constexpr bool operator < (const Fp& r) const noexcept { return this->val < r.val; } friend constexpr istream& operator >> (istream& is, Fp& x) noexcept { is >> x.val; x.val %= MOD; if (x.val < 0) x.val += MOD; return is; } friend constexpr ostream& operator << (ostream& os, const Fp& x) noexcept { return os << x.val; } friend constexpr Fp modpow(const Fp& a, long long n) noexcept { Fp res=1,r=a; while(n){ if(n&1) res*=r; r*=r; n>>=1; } return res; } friend constexpr Fp modinv(const Fp& r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b, swap(a, b); u -= t * v, swap(u, v); } return Fp(u); } ll get(){ return val; } explicit operator bool()const{ return val; } }; template< uint32_t mod, bool fast = false > struct MontgomeryModInt { using mint = MontgomeryModInt; using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; static constexpr u32 get_r() { u32 ret = mod; for(i32 i = 0; i < 4; i++) ret *= 2 - mod * ret; return ret; } static constexpr u32 r = get_r(); static constexpr u32 n2 = -u64(mod) % mod; static_assert(r * mod == 1, "invalid, r * mod != 1"); static_assert(mod < (1 << 30), "invalid, mod >= 2 ^ 30"); static_assert((mod & 1) == 1, "invalid, mod % 2 == 0"); u32 a; MontgomeryModInt() : a{} {} MontgomeryModInt(const i64 &x) : a(reduce(u64(fast ? x : (x % mod + mod)) * n2)) {} static constexpr u32 reduce(const u64 &b) { return u32(b >> 32) + mod - u32((u64(u32(b) * r) * mod) >> 32); } constexpr mint& operator+=(const mint &p) { if(i32(a += p.a - 2 * mod) < 0) a += 2 * mod; return *this; } constexpr mint& operator-=(const mint &p) { if(i32(a -= p.a) < 0) a += 2 * mod; return *this; } constexpr mint& operator*=(const mint &p) { a = reduce(u64(a) * p.a); return *this; } constexpr mint& operator/=(const mint &p) { *this *= modinv(p); return *this; } constexpr mint operator-() const { return mint() - *this; } constexpr mint operator+(const mint &p) const { return mint(*this) += p; } constexpr mint operator-(const mint &p) const { return mint(*this) -= p; } constexpr mint operator*(const mint &p) const { return mint(*this) *= p; } constexpr mint operator/(const mint &p) const { return mint(*this) /= p; } constexpr bool operator==(const mint &p) const { return (a >= mod ? a - mod : a) == (p.a >= mod ? p.a - mod : p.a); } constexpr bool operator!=(const mint &p) const { return (a >= mod ? a - mod : a) != (p.a >= mod ? p.a - mod : p.a); } u32 get() const { u32 ret = reduce(a); return ret >= mod ? ret - mod : ret; } friend constexpr MontgomeryModInt modpow(const MontgomeryModInt &x,u64 n) noexcept { MontgomeryModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend constexpr MontgomeryModInt modinv(const MontgomeryModInt &r) noexcept { u64 a = r.get(), b = mod, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b, swap(a, b); u -= t * v, swap(u, v); } return MontgomeryModInt(u); } friend ostream &operator<<(ostream &os, const mint &p) { return os << p.get(); } friend istream &operator>>(istream &is, mint &a) { i64 t; is >> t; a = mint(t); return is; } static constexpr u32 getmod() { return mod; } }; ll mod(ll a,ll MOD){ if(a<0) a+=MOD; return a%MOD; } ll modpow(ll a,ll n,ll mod){ ll res=1; a%=mod; while (n>0){ if (n & 1) res*=a; a *= a; a%=mod; n >>= 1; res%=mod; } return res; } vector

prime_factorize(ll N) { vector

res; for (ll a = 2; a * a <= N; ++a) { if (N % a != 0) continue; ll ex = 0; while(N % a == 0){ ++ex; N /= a; } res.push_back({a, ex}); } if (N != 1) res.push_back({N, 1}); return res; } ll modinv(ll a, ll mod) { ll b = mod, u = 1, v = 0; while (b) { ll t = a/b; a -= t * b, swap(a, b); u -= t * v, swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } ll extGcd(ll a, ll b, ll &p, ll &q) { if (b == 0) { p = 1; q = 0; return a; } ll d = extGcd(b, a%b, q, p); q -= a/b * p; return d; } P ChineseRem(const vector &b, const vector &m) { ll r = 0, M = 1; for (int i = 0; i < (int)b.size(); ++i) { ll p, q; ll d = extGcd(M, m[i], p, q); if ((b[i] - r) % d != 0) return make_pair(0, -1); ll tmp = (b[i] - r) / d * p % (m[i]/d); r += M * tmp; M *= m[i]/d; } return make_pair(mod(r, M), M); } //fast Input by yosupo #include #include #include #include #include #include #include #include #include #include namespace fastio{ /* quote from yosupo's submission in Library Checker */ int bsr(unsigned int n) { return 8 * (int)sizeof(unsigned int) - 1 - __builtin_clz(n); } // @param n `1 <= n` // @return maximum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsr(unsigned long n) { return 8 * (int)sizeof(unsigned long) - 1 - __builtin_clzl(n); } // @param n `1 <= n` // @return maximum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsr(unsigned long long n) { return 8 * (int)sizeof(unsigned long long) - 1 - __builtin_clzll(n); } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsr(unsigned __int128 n) { unsigned long long low = (unsigned long long)(n); unsigned long long high = (unsigned long long)(n >> 64); return high ? 127 - __builtin_clzll(high) : 63 - __builtin_ctzll(low); } namespace internal { template using is_signed_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using is_integral = typename std::conditional::value || internal::is_signed_int128::value || internal::is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed_int = typename std::conditional<(is_integral::value && std::is_signed::value) || is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional<(is_integral::value && std::is_unsigned::value) || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional< is_signed_int128::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; template using is_integral_t = std::enable_if_t::value>; template using is_signed_int_t = std::enable_if_t::value>; template using is_unsigned_int_t = std::enable_if_t::value>; template using to_unsigned_t = typename to_unsigned::type; } // namespace internal struct Scanner { public: Scanner(const Scanner&) = delete; Scanner& operator=(const Scanner&) = delete; Scanner(FILE* fp) : fd(fileno(fp)) {} void read() {} template void read(H& h, T&... t) { bool f = read_single(h); assert(f); read(t...); } int read_unsafe() { return 0; } template int read_unsafe(H& h, T&... t) { bool f = read_single(h); if (!f) return 0; return 1 + read_unsafe(t...); } int close() { return ::close(fd); } private: static constexpr int SIZE = 1 << 15; int fd = -1; std::array line; int st = 0, ed = 0; bool eof = false; bool read_single(std::string& ref) { if (!skip_space()) return false; ref = ""; while (true) { char c = top(); if (c <= ' ') break; ref += c; st++; } return true; } bool read_single(double& ref) { std::string s; if (!read_single(s)) return false; ref = std::stod(s); return true; } template ::value>* = nullptr> bool read_single(T& ref) { if (!skip_space<50>()) return false; ref = top(); st++; return true; } template * = nullptr, std::enable_if_t::value>* = nullptr> bool read_single(T& sref) { using U = internal::to_unsigned_t; if (!skip_space<50>()) return false; bool neg = false; if (line[st] == '-') { neg = true; st++; } U ref = 0; do { ref = 10 * ref + (line[st++] & 0x0f); } while (line[st] >= '0'); sref = neg ? -ref : ref; return true; } template * = nullptr, std::enable_if_t::value>* = nullptr> bool read_single(U& ref) { if (!skip_space<50>()) return false; ref = 0; do { ref = 10 * ref + (line[st++] & 0x0f); } while (line[st] >= '0'); return true; } bool reread() { if (ed - st >= 50) return true; if (st > SIZE / 2) { std::memmove(line.data(), line.data() + st, ed - st); ed -= st; st = 0; } if (eof) return false; auto u = ::read(fd, line.data() + ed, SIZE - ed); if (u == 0) { eof = true; line[ed] = '\0'; u = 1; } ed += int(u); line[ed] = char(127); return true; } char top() { if (st == ed) { bool f = reread(); assert(f); } return line[st]; } template bool skip_space() { while (true) { while (line[st] <= ' ') st++; if (ed - st > TOKEN_LEN) return true; if (st > ed) st = ed; for (auto i = st; i < ed; i++) { if (line[i] <= ' ') return true; } if (!reread()) return false; } } }; //fast Output by ei1333 /** * @brief Printer(高速出力) */ struct Printer { public: explicit Printer(FILE *fp) : fp(fp) {} ~Printer() { flush(); } template< bool f = false, typename T, typename... E > void write(const T &t, const E &... e) { if(f) write_single(' '); write_single(t); write< true >(e...); } template< typename... T > void writeln(const T &...t) { write(t...); write_single('\n'); } void flush() { fwrite(line, 1, st - line, fp); st = line; } private: FILE *fp = nullptr; static constexpr size_t line_size = 1 << 16; static constexpr size_t int_digits = 20; char line[line_size + 1] = {}; char small[32] = {}; char *st = line; template< bool f = false > void write() {} void write_single(const char &t) { if(st + 1 >= line + line_size) flush(); *st++ = t; } template< typename T, enable_if_t< is_integral< T >::value, int > = 0 > void write_single(T s) { if(st + int_digits >= line + line_size) flush(); if(s == 0) { write_single('0'); return; } if(s < 0) { write_single('-'); s = -s; } char *mp = small + sizeof(small); typename make_unsigned< T >::type y = s; size_t len = 0; while(y > 0) { *--mp = y % 10 + '0'; y /= 10; ++len; } memmove(st, mp, len); st += len; } void write_single(const string &s) { for(auto &c : s) write_single(c); } void write_single(const char *s) { while(*s != 0) write_single(*s++); } template< typename T > void write_single(const vector< T > &s) { for(size_t i = 0; i < s.size(); i++) { if(i) write_single(' '); write_single(s[i]); } } }; }; //namespace fastio using mint=MontgomeryModInt<998244353>; int main(){ fastio::Scanner sc(stdin); fastio::Printer pr(stdout); #define in(...) sc.read(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define STR(...) string __VA_ARGS__;in(__VA_ARGS__) #define out(...) pr.write(__VA_ARGS__) #define outln(...) pr.writeln(__VA_ARGS__) #define outspace(...) pr.write(__VA_ARGS__),pr.write(' ') #define rall(v) (v).rbegin(), (v).rend() #define fi first #define se second /* ここでは左上を(0,0)として考える 以下のパターン ①上下、左右 2^H+2^W ②x+yが偶数に上下x+yが奇数に左右、その逆、同じ行、列には左右、上下はない。2*2^H*2^W ③上左、右下はx+y mod gでそれぞれ決まる、2*2^g ④上右、左下はx-y mod g 2*2^g 1,3,4はいかなるgについても存在する 2はgが偶数の時のみ(奇数の時はx+y≡2 mod gとx+y≡0 mod 2が同値でない) 1,3,4で重複が8(4方向*2) 1,2,3,4で重複が16(1,3,4の8+2*(2の縦横それぞれ1方向のケース(=4))) これ以外のケースを考える 1,2,3,4を満たしていない数え上げたい対象は4*4グリッドの周期になる (証明は解説に任せる(え?)) 当然、ここまでくればグリッドを全探索すれば良い (このグリッドには2ヶ所同じ方向があって、探索するのは4^8通り) 全体のグリッドがトーラスであることを考えると、ちゃんと4*4のグリッドでピッタリ敷き詰められる必要があり gが4の倍数の時しかこのケースは存在しない このケースのうち1,2,3,4と被らないものは48通り存在する。(全探索) K>0の時 すでに書き込まれた数によって固定されるものを考えれば良い、実装はとてつもなく大変 コメント: 割り当てる方向の個数が3つ以上の数え上げは、②を計算することにより残るものが 定数個になるというの、とても難しかった、めっちゃadhoc どうやって作問したんですか? */ auto calc=[](map &mp){ map d; for(auto& [key,val]:mp) d[val]++; int ret=0; for(auto& [c,cnt]:d) if(cnt>0) ret++; return ret; }; INT(t); assert(1<=t&&t<=100); while(t--){ LL(h,w,k); assert(1<=h&&h<=1000000000&&1<=w&&w<=1000000000&&0<=k&&k<=min(2000ll,h*w)); ll g=gcd(h,w); mint ans=0; if(k==0){ if(g%4==0){ ans=modpow(mint(2),h)+modpow(mint(2),w)+modpow(mint(2),h+w+1)+modpow(mint(2),g)*4+32; }else if(g%4==2){ ans=modpow(mint(2),h)+modpow(mint(2),w)+modpow(mint(2),h+w+1)+modpow(mint(2),g)*4-16; }else{ ans=modpow(mint(2),h)+modpow(mint(2),w)+modpow(mint(2),g)*4-8; } outln(ans.get()); continue; } swap(h,w); vector x(k),y(k); vector d(k); bool hasver=false,hashor=false; FOR(i,k){ in(y[i],x[i],d[i]); y[i]=w-y[i]-1; x[i]=h-x[i]-1; assert(0<=x[i]&&x[i]> X,Y; FOR(i,k){ X[x[i]].push_back(d[i]); Y[y[i]].push_back(d[i]); } bool ret0h=false,ret0w=false; ll h1=h,w1=w; for(auto& [key,val]:X){ sort(all(val)); val.erase(unique(all(val)),val.end()); if(val.size()>1) ret0h=true; for(auto& element:val){ if(element!='L'&&element!='R') ret0h=true; } if(ret0h) break; h1--; } for(auto& [key,val]:Y){ sort(all(val)); val.erase(unique(all(val)),val.end()); if(val.size()>1) ret0w=true; for(auto& element:val){ if(element!='U'&&element!='D') ret0w=true; } if(ret0w) break; w1--; } if(!ret0h) ans+=modpow(mint(2),h1); if(!ret0w) ans+=modpow(mint(2),w1); } //case 2 if(g%2==0){ bool oddver=false,oddhor=false,evenver=false,evenhor=false; FOR(i,k){ if((x[i]+y[i])%2){ if(d[i]=='L'||d[i]=='R') oddhor=true; else oddver=true; }else{ if(d[i]=='L'||d[i]=='R') evenhor=true; else evenver=true; } } if(!(oddver&&oddhor)&&!(evenver&&evenhor)&&!(oddver&&evenver)&&!(oddhor&&evenhor)){ if(evenver||oddhor){ bool ret0=false; map X,Y; FOR(i,k){ if((x[i]+y[i])%2){ if(X.find(x[i])==X.end()){ X[x[i]]=d[i]; }else if(X[x[i]]!=d[i]){ ret0=true; } }else{ if(Y.find(y[i])==Y.end()){ Y[y[i]]=d[i]; }else if(Y[y[i]]!=d[i]){ ret0=true; } } } if(!ret0){ ans+=modpow(mint(2),h+w-X.size()-Y.size())-(2-calc(X))*(2-calc(Y)); } } if(evenhor||oddver){ bool ret0=false; map X,Y; FOR(i,k){ if((x[i]+y[i])%2==0){ if(X.find(x[i])==X.end()){ X[x[i]]=d[i]; }else if(X[x[i]]!=d[i]){ ret0=true; } }else{ if(Y.find(y[i])==Y.end()){ Y[y[i]]=d[i]; }else if(Y[y[i]]!=d[i]){ ret0=true; } } } if(!ret0){ ans+=modpow(mint(2),h+w-X.size()-Y.size())-(2-calc(X))*(2-calc(Y)); } } } } //case 3 { bool ret0=false; map mp; map dc; FOR(i,k){ if(mp.find((x[i]+y[i])%g)==mp.end()){ mp[(x[i]+y[i])%g]=d[i]; }else if(mp[(x[i]+y[i])%g]!=d[i]){ ret0=true; } dc[d[i]]++; } if((dc['L']>0||dc['U']>0)&&dc['R']==0&&dc['D']==0&&!ret0){ ans+=modpow(mint(2),g-mp.size())-2+dc.size(); }else if((dc['R']>0||dc['D']>0)&&dc['L']==0&&dc['U']==0&&!ret0){ ans+=modpow(mint(2),g-mp.size())-2+dc.size(); } } //case 4 { bool ret0=false; map mp; map dc; FOR(i,k){ if(mp.find((x[i]+(g-1)*y[i])%g)==mp.end()){ mp[(x[i]+(g-1)*y[i])%g]=d[i]; }else if(mp[(x[i]+(g-1)*y[i])%g]!=d[i]){ ret0=true; } dc[d[i]]++; } if((dc['L']>0||dc['U']>0)&&dc['R']==0&&dc['D']==0&&!ret0){ ans+=modpow(mint(2),g-mp.size())-2+dc.size(); }else if((dc['R']>0||dc['D']>0)&&dc['L']==0&&dc['U']==0&&!ret0){ ans+=modpow(mint(2),g-mp.size())-2+dc.size(); } } //case muzui if(g%4==0){ vector> grid(4,vector(4,'.')); bool ret0=false; FOR(i,k){ if(grid[x[i]%4][y[i]%4]=='.'){ grid[x[i]%4][y[i]%4]=d[i]; }else if(grid[x[i]%4][y[i]%4]!=d[i]){ ret0=true; } } FOR(i,4) FOR(j,2){ if(grid[i][j]!='.'&&grid[(i+2)%4][(j+2)%4]!='.'){ if(grid[i][j]!=grid[(i+2)%4][(j+2)%4]) ret0=true; }else if(grid[i][j]!='.'){ grid[(i+2)%4][(j+2)%4]=grid[i][j]; }else if(grid[(i+2)%4][(j+2)%4]!='.'){ grid[i][j]=grid[(i+2)%4][(j+2)%4]; } } if(!ret0){ //すでに1,2,3,4との被りを省いた48通りのうちマッチするものを探し出す //このコードを書いてる人は今書いてる時点ですでに疲れており、48通りのグリッドを書き出したくない //書き出すグリッドの個数は6個に絞ることができる(トーラス上で8通りのずらす方法がそれぞれある) vector>> solution={ { {'R','L','R','U'}, {'R','L','D','L'}, {'R','U','R','L'}, {'D','L','R','L'} }, { {'L','U','L','R'}, {'L','R','D','R'}, {'L','R','L','U'}, {'D','R','L','R'} }, { {'U','U','U','R'}, {'L','D','D','D'}, {'U','R','U','U'}, {'D','D','L','D'} }, { {'U','U','U','L'}, {'D','D','R','D'}, {'U','L','U','U'}, {'R','D','D','D'} }, { {'U','U','L','R'}, {'L','R','D','D'}, {'L','R','U','U'}, {'D','D','L','R'} }, { {'L','U','U','R'}, {'L','D','D','R'}, {'U','R','L','U'}, {'D','R','L','D'} } }; FOR(i,6) FOR(a,4) FOR(b,2){ auto& s=solution[i]; bool ok=true; FOR(x,4) FOR(y,4){ if(grid[x][y]!='.'&&grid[x][y]!=s[(x+a)%4][(y+b)%4]) ok=false; } if(ok) ans+=1; } } } outln(ans.get()); } }