結果
| 問題 | No.3600 Moving Queen Many Times |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-07-24 23:05:47 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 2,111 ms / 7,000 ms |
| + 100µs | |
| コード長 | 8,263 bytes |
| 記録 | |
| コンパイル時間 | 5,896 ms |
| コンパイル使用メモリ | 423,288 KB |
| 実行使用メモリ | 10,096 KB |
| 最終ジャッジ日時 | 2026-07-24 23:06:12 |
| 合計ジャッジ時間 | 25,481 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 75 |
ソースコード
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#define ll long long
#define ld long double
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define vi vector<int>
#define vl vector<ll>
#define vd vector<double>
#define vb vector<bool>
#define vs vector<string>
#define vc vector<char>
#define ull unsigned long long
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
template<class T, class U>
inline bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T, class U>
inline bool chmin(T &a, const U &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// #define ll int
// #define ll int128_t
// #define ll int256_t
// #define ll cpp_int
constexpr ll inf = (1ll << 62);
// constexpr ll inf = (1 << 30);
// const double PI=3.1415926535897932384626433832795028841971;
uint32_t xor_x = 123456789, xor_y = 362436069, xor_z = 521288629, xor_w = 88675123;
inline uint32_t xor_next() {
uint32_t t = xor_x ^ (xor_x << 11);
xor_x = xor_y; xor_y = xor_z; xor_z = xor_w;
return xor_w = (xor_w ^ (xor_w >> 19)) ^ (t ^ (t >> 8));
}
inline int rnd(int max_val) { return xor_next() % max_val; }
// struct Timer {
// std::chrono::steady_clock::time_point start_time;
// Timer() {
// reset();
// }
// // 測定の起点リセット用
// void reset() {
// start_time = std::chrono::steady_clock::now();
// }
// // スタートからの経過時間をミリ秒(msec)で返す
// long long get_ms() const {
// auto now = std::chrono::steady_clock::now();
// return std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time).count();
// }
// };
// ll rui(ll a,ll b){
// if(b==0)return 1;
// if(b%2==1) return a*rui(a*a,b/2);
// return rui(a*a,b/2);
// }
// vl fact;
// ll kai(ll n){
// fact.resize(n,1);
// rep(i,n-1)fact[i+1]=fact[i]*(i+1);
// }
// using mint = ld;
using mint = modint998244353;//static_modint<998244353>
// using mint = modint1000000007;//static_modint<1000000007>
// using mint = static_modint<922267487>; // 多分落とされにくい NOT ntt-friendly
// using mint = static_modint<469762049>; // ntt-friendly
// using mint = static_modint<167772161>; // ntt-friendly
// using mint = modint;//mint::set_mod(mod);
// ll const mod=1000000007ll;
// ll const mod=998244353ll;
// ll modrui(ll a,ll b,ll mod){
// a%=mod;
// if(b==0)return 1;
// if(b%2==1) return a*modrui(a*a%mod,b/2,mod)%mod;
// return modrui(a*a%mod,b/2,mod)%mod;
// }
// void incr(vl &v,ll n){// n進法
// ll k=v.size();
// v[k-1]++;
// ll now=k-1;
// while (v[now]>=n)
// {
// v[now]=0;
// if(now==0)break;
// v[now-1]++;
// now--;
// }
// return;
// }
// vector<mint> fact,invf;
// void init_modfact(ll sz){
// fact.resize(sz);
// invf.resize(sz);
// fact[0]=1;
// rep(i,sz-1){
// fact[i+1]=fact[i]*(i+1);
// }
// invf[sz-1]=1/fact[sz-1];
// for(ll i=sz-2; i>=0; i--){
// invf[i]=invf[i+1]*(i+1);
// }
// }
// mint choose(ll n,ll r){
// if(n<r || r<0 || n<0)return 0;
// return fact[n]*invf[r]*invf[n-r];
// }
// vector<mint> modpow,invpow;
// void init_modpow(ll x,ll sz){
// mint inv=1/mint(x);
// modpow.assign(sz,1);
// invpow.assign(sz,1);
// rep(i,sz-1){
// modpow[i+1]=modpow[i]*x;
// invpow[i+1]=invpow[i]*inv;
// }
// }
// long long phi(long long n) {// O(sqrt(n))
// long long res = n;
// for (long long i = 2; i * i <= n; i++) {
// if (n % i == 0) {
// res -= res / i;
// while (n % i == 0) n /= i;
// }
// }
// if (n > 1) res -= res / n;
// return res;
// }
//http://atcoder.jp/contests/abc236/submissions/73613608
//https://judge.yosupo.jp/submission/355510
template <class S>
struct Matrix {
using T = typename S::T;
int h, w;
std::vector<std::vector<T>> val;
// zero() で初期化
Matrix(int h, int w) : h(h), w(w), val(h, std::vector<T>(w, S::zero())) {}
// 単位行列の生成(対角成分を one() にする)
static Matrix eye(int n) {
Matrix res(n, n);
for (int i = 0; i < n; i++) res.val[i][i] = S::one();
return res;
}
// 行列同士の積
Matrix operator*(const Matrix& a) const {
assert(w == a.h);
Matrix res(h, a.w);
for (int i = 0; i < h; i++) {
for (int k = 0; k < w; k++) {
for (int j = 0; j < a.w; j++) {
// res[i][j] += val[i][k] * a[k][j] の抽象化
res.val[i][j] = S::add(res.val[i][j], S::mul(val[i][k], a.val[k][j]));
}
}
}
return res;
}
// ベクトルへの線形変換 (Matrix * Vector)
std::vector<T> operator*(const std::vector<T>& vec) const {
assert(w == vec.size());
std::vector<T> res(h, S::zero());
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
res[i] = S::add(res[i], S::mul(val[i][j], vec[j]));
}
}
return res;
}
// 行列の累乗(ダブリング)
Matrix pow(long long k) const {
assert(h == w);
Matrix res = eye(h);
Matrix a = *this;
while (k > 0) {
if (k & 1) res = res * a;
a = a * a;
k >>= 1;
}
return res;
}
};
struct Semiring {
using T = mint; // modint998244353 などに変えるだけ
static T add(T a, T b) { return a+b; }
static T mul(T a, T b) { return a*b; }
static T zero() { return 0; }// addの単位元
static T one() { return 1; }// mulの単位元
};
void solve(){
ll h,w,sx,sy,gx,gy,k;
cin >> h >> w >> sx >> sy >> gx >> gy >> k;
sx--;
sy--;
gx--;
gy--;
Matrix<Semiring> f(h*w,h*w);
rep(i,h)rep(j,w){
vector dp(1<<(h*w),vector(h*w,0));
ll stt=i*w+j;
dp[0][stt]=1;
rep(bit,1<<(h*w))rep(pi,h)rep(pj,w)rep(ni,h)rep(nj,w){
if(ni==pi && nj==pj)continue;
ll now=pi*w+pj;
ll nxt=ni*w+nj;
if(pi==ni || pj==nj || pi+pj==ni+nj || pi-pj==ni-nj){
if(bit&(1<<nxt))continue;
dp[bit|(1<<nxt)][nxt]+=dp[bit][now];
}
}
rep(ni,h)rep(nj,w){
ll nxt=ni*w+nj;
f.val[nxt][stt]=dp[(1<<(h*w))-1][nxt];
}
}
// rep(i,h*w)rep(j,h*w){
// cerr << f.val[i][j].val() << " \n"[j==h*w-1];
// }cerr << endl;
f=f.pow(k/(h*w));
k%=h*w;
vector<mint> cnt(h*w,0);
cnt[sx*w+sy]=1;
cnt=f*cnt;
// rep(i,h*w){
// cerr << cnt[i].val() << " \n"[i==h*w-1];
// }cerr << endl;
mint ans=0;
rep(i,h*w)rep(j,h*w)f.val[i][j]=0;
rep(i,h)rep(j,w){
vector dp(1<<(h*w),vector(h*w,0));
ll stt=i*w+j;
dp[0][stt]=1;
rep(bit,1<<(h*w))rep(pi,h)rep(pj,w)rep(ni,h)rep(nj,w){
if(ni==pi && nj==pj)continue;
ll now=pi*w+pj;
ll nxt=ni*w+nj;
if(pi==ni || pj==nj || pi+pj==ni+nj || pi-pj==ni-nj){
if(bit&(1<<nxt))continue;
dp[bit|(1<<nxt)][nxt]+=dp[bit][now];
}
}
rep(bit,1<<(h*w)){
if(__builtin_popcount(bit)==k){
{
ll nxt=gx*w+gy;
ans+=cnt[stt]*dp[bit][nxt];
}
}
}
}
cout << ans.val() << endl;
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// ll mx=450;
// vc fl(mx+1,0);
// for(ll d=2;d<=mx;d++){
// if(fl[d])continue;
// ll x=d;
// ps.push_back(x);
// while(x<=mx){
// fl[x]=1;
// x+=d;
// }
// }
ll t = 1;
// cin >> t;
while (t--){
solve();
}
}