跳到正文

题解

题解 AT_ddcc2020_qual_f DISCOSMOS

meyi约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年12月18日
收录
2026年5月10日
更新
2026年11月17日
篇幅
约 9 分钟

版本

  1. v1当前

不妨当成每个点一步走 TT 格,那么第 ii 行和第 (i+T) mod H(i + T) \bmod H 行在同一个等价类里,同理第 jj 列和第 (j+T) mod W(j + T) \bmod W 列也在同一个等价类里。

考虑 (i,j)(i, j) 这个格子所在的等价类,不妨重新映射坐标使得第 ii 下方相邻的行就是第 (i+T) mod H(i + T) \bmod H 行,同理令第 jj 列右方相邻的列就是第 (j+T) mod W(j + T) \bmod W 列,那么转化为了右移和下移均只走一格的问题。而这样的等价类有 gcd⁡(H,T)×gcd⁡(W,T)\gcd(H, T) \times \gcd(W, T) 个,所有等价类的答案相同,求出单个等价类的答案后快速幂即可得到原问题的答案。

一个等价类有 n=Hgcd⁡(H,T)n = \dfrac{H}{\gcd(H, T)} 行 m=Wgcd⁡(W,T)m = \dfrac{W}{\gcd(W, T)} 列,接下来开始分类讨论:

  • 全为 . 是一种合法方案。
  • 若等价类中不存在 >,则一列要么同时为 v,要么同时为 .,除去全为 . 的情况有 2m−12 ^ m - 1 种方案。
  • 若等价类中不存在 v,则一行要么同时为 >,要么同时为 .,除去全为 . 的情况有 2n−12 ^ n - 1 种方案。
  • 若等价类中同时存在 > 和 v,那么等价类中必定不存在 .,因为要么 . 所在的列全为 .,要么所在的行全为 .,这两种情况分别与已有的 > 和 v 产生矛盾。考虑 (i−1,j)(i - 1, j) 和 (i,j−1)(i, j - 1) 这两个格子,由于移动一步后 (i,j)(i, j) 这个格子上必须恰好有一个机器人,所以 (i−1,j)(i - 1, j) 和 (i,j−1)(i, j - 1) 要么同时为 >,要么同时为 v,这告诉我们 (i,j)(i, j) 和 (i+1,j−1)(i + 1, j - 1) 同属于一个子等价类,且一个子等价类要么全为 >,要么全为 v。考虑 i+k≡i(modn)i + k \equiv i \pmod n 和 j−k≡j(modm)j - k \equiv j \pmod m 这两个同余方程,显然有最小正整数公共解 k=lcm⁡(n,m)k = \operatorname{lcm}(n, m),因此一个子等价类中有 lcm⁡(n,m)\operatorname{lcm}(n, m) 个格子,故子等价类的数量为 n×mlcm⁡(n,m)=gcd⁡(n,m)\dfrac{n\times m}{\operatorname{lcm}(n, m)} = \gcd(n, m)。除去全为 > 和全为 v 的情况有 2gcd⁡(n,m)−22 ^{\gcd(n, m)} - 2 种方案。

故一个等价类的总方案数为 1+(2m−1)+(2n−1)+(2gcd⁡(n,m)−2)=2m+2n+2gcd⁡(n,m)−31 + (2 ^ m - 1) + (2 ^ n - 1) + (2 ^{\gcd(n, m)} - 2) = 2 ^ m + 2 ^ n + 2 ^{\gcd(n, m)} - 3,原问题的答案为 (2m+2n+2gcd⁡(n,m)−3)gcd⁡(H,T)×gcd⁡(W,T)(2 ^ m + 2 ^ n + 2 ^{\gcd(n, m)} - 3) ^{\gcd(H, T) \times \gcd(W, T)}。

C++
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#define ALL(v) v.begin(),v.end()
#define For(i,_) for(int i=0,i##end=_;i<i##end;++i) // [0,_)
#define FOR(i,_,__) for(int i=_,i##end=__;i<i##end;++i) // [_,__)
#define Rep(i,_) for(int i=(_)-1;i>=0;--i) // [0,_)
#define REP(i,_,__) for(int i=(__)-1,i##end=_;i>=i##end;--i) // [_,__)
typedef long long ll;
typedef unsigned long long ull;
#define V vector
#define pb push_back
#define pf push_front
#define qb pop_back
#define qf pop_front
#define eb emplace_back
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
#define fi first
#define se second
const int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}},inf=0x3f3f3f3f,mod=1e9+7;
const ll infl=0x3f3f3f3f3f3f3f3fll;
template<class T>inline bool ckmin(T &x,const T &y){return x>y?x=y,1:0;}
template<class T>inline bool ckmax(T &x,const T &y){return x<y?x=y,1:0;}
namespace{int init=[](){return cin.tie(nullptr)->sync_with_stdio(false),0;}();}
template<int p>
struct modint{
    int val;
    inline modint(int v=0):val(v){}
    inline modint& operator=(int v){val=v;return *this;}
    inline modint& operator+=(const modint&k){val=val+k.val>=p?val+k.val-p:val+k.val;return *this;}
    inline modint& operator-=(const modint&k){val=val-k.val<0?val-k.val+p:val-k.val;return *this;}
    inline modint& operator*=(const modint&k){val=int(1ll*val*k.val%p);return *this;}
    inline modint& operator^=(int k){modint r(1),b=*this;for(;k;k>>=1,b*=b)if(k&1)r*=b;val=r.val;return *this;}
    inline modint& operator/=(modint k){return *this*=(k^=p-2);}
    inline modint& operator+=(int k){val=val+k>=p?val+k-p:val+k;return *this;}
    inline modint& operator-=(int k){val=val<k?val-k+p:val-k;return *this;}
    inline modint& operator*=(int k){val=int(1ll*val*k%p);return *this;}
    inline modint& operator/=(int k){return *this*=((modint(k))^=p-2);}
    template<class T>friend modint operator+(modint a,T b){return a+=b;}
    template<class T>friend modint operator-(modint a,T b){return a-=b;}
    template<class T>friend modint operator*(modint a,T b){return a*=b;}
    template<class T>friend modint operator/(modint a,T b){return a/=b;}
    friend modint operator^(modint a,int b){return a^=b;}
    friend bool operator==(modint a,int b){return a.val==b;}
    friend bool operator!=(modint a,int b){return a.val!=b;}
    inline bool operator!()const{return !val;}
    inline modint operator-()const{return val?modint(p-val):modint(0);}
    inline modint operator++(int){modint t=*this;*this+=1;return t;}
    inline modint& operator++(){return *this+=1;}
    inline modint operator--(int){modint t=*this;*this-=1;return t;}
    inline modint& operator--(){return *this-=1;}
};
using mi=modint<mod>;
int main(){
    int t_case=1;
    //scanf("%d",&t_case);
    while(t_case--){
        int H,T,W;
        scanf("%d%d%d",&H,&W,&T);
        int x=gcd(H,T),y=gcd(W,T),n=H/x,m=W/y;
        printf("%d\n",((mi(2)^m)+(mi(2)^n)+(mi(2)^gcd(n,m))-3)^(1ll*x*y%(mod-1)));
    }
    return 0;
}
原文