zoukankan      html  css  js  c++  java
  • Codeforces-1332E Height All the Same

    Codeforces-1332E Height All the Same

    Alice has got addicted to a game called Sirtet recently.

    In Sirtet, player is given an (n imes m) grid. Initially (a_{i,j}) cubes are stacked up in the cell ((i,j)). Two cells are called adjacent if they share a side. Player can perform the following operations:

    • stack up one cube in two adjacent cells;
    • stack up two cubes in one cell.

    Cubes mentioned above are identical in height.

    Here is an illustration of the game. States on the right are obtained by performing one of the above operations on the state on the left, and grey cubes are added due to the operation.

    img

    Player's goal is to make the height of all cells the same (i.e. so that each cell has the same number of cubes in it) using above operations.

    Alice, however, has found out that on some starting grids she may never reach the goal no matter what strategy she uses. Thus, she is wondering the number of initial grids such that

    • (L le a_{i,j} le R) for all (1 le i le n), (1 le j le m);
    • player can reach the goal using above operations.

    Please help Alice with it. Notice that the answer might be large, please output the desired value modulo (998,244,353).

    Input

    The only line contains four integers (n), (m), (L) and (R) ((1le n,m,L,R le 10^9), (L le R), (n cdot m ge 2)).

    Output

    Output one integer, representing the desired answer modulo (998,244,353).

    Examples

    Input

    2 2 1 1
    

    Output

    1
    

    Input

    1 2 1 2
    

    Output

    2
    

    Note

    In the first sample, the only initial grid that satisfies the requirements is (a_{1,1}=a_{2,1}=a_{1,2}=a_{2,2}=1). Thus the answer should be (1).

    In the second sample, initial grids that satisfy the requirements are (a_{1,1}=a_{1,2}=1) and (a_{1,1}=a_{1,2}=2). Thus the answer should be (2).

    题解

    CF的官方题解写的很清楚,这里就简单翻译一下了

    1. 每格实际的方块数不重要,只有奇偶性对答案有影响,因为对于所有奇偶性相同的格子,我们可以通过操作2把他们变成同样高
    2. 可以改变任意一对方格的奇偶性,因为我们可以通过一条路径(p = w_0w_1dots w_k),其中(w_0=u,w_k=v)来改变u,v的奇偶性而不影响路径中其他格子的奇偶性(因为都被操作了两次)
    3. 如果(n*m)是奇数,那么无论初始局面是怎样的,都可以达到全部高度一样,因为(n*m)是奇数,所以如果有奇数个方块数为偶数的格子,那么一定有偶数个方块数为奇数的格子,反之亦然,那么我们直接改变总数是偶数个的那边格子的奇偶性即可,两两配对即可完成
    4. 如果(n*m)是偶数,如果(sum_{i=1}^n sum_{j=1}^m a_{i,j})是偶数,那么可以达到所有高度一样,如果是奇数则不行.因为(n*m)是偶数,所以我们只能是偶数个方块数为偶数的格子和偶数个方块数为奇数的格子才满足要求,这样也就导致了总和是偶数而非奇数

    对于(n*m)是奇数的答案很简单,直接输出即可,关键在于如何算出(n*m)是偶数时(sum_{i=1}^n sum_{j=1}^m a_{i,j})的方案数,对于这一类题有一个通用的办法,就是考虑让合法的局面和不合法的局面配对.

    我们先把([l,r])降到([0,r-l])如果(r-l)是奇数,那么我们对于任意一个局面,随便找个格子,使其异或1,那么整个总和的奇偶性就会发生改变,也就是找到了一个对应的局面.而因为(r-l)是奇数,(r-l oplus 1)不会超出范围,

    如果(r-l)是偶数,显然对于任意一个局面,只有每个格子的方块数全是(r-l)的找不到格子对应,无法通过异或1改变奇偶性(超出(r-l)范围),所以合法状态比非法状态多1.

    这样所有情况就处理完了,感觉注意到第一第二点就能想出后面的部分了,可惜想歪了,总想着怎么dp+矩阵

    代码

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int N = 2e5 + 50;
    const ll mod = 998244353;
    ll qpow(ll a, ll b) {
        ll ans = 1;
        while (b) {
            if (b & 1) ans = ans * a % mod;
            a = a * a % mod;
            b >>= 1;
        }
        return ans;
    }
    int main() {
        
        ll n, m, l, r;
        cin >> n >> m >> l >> r;
        if (n * m % 2 == 1) {
            printf("%lld
    ", qpow((r - l + 1), n * m));
        }
        else {
            ll ans;
            if ((r - l + 1) % 2 == 0) ans = qpow(r - l + 1, n * m) * ((mod + 1) / 2) % mod;
            else ans = (qpow(r - l + 1, n * m) + 1) * ((mod + 1) / 2) % mod; 
            printf("%lld
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    js实现考试倒计时
    freemarker常见语法大全
    页面修改图片路径
    freemaker页面字符串特殊字符显示异常处理
    win10家庭版远程连接 要求的函数不受支持
    mysql出现提示错误10061的解决方法
    freemark
    面对众多的前端框架,你该如何学习?
    HBase 入门
    Hive 优化
  • 原文地址:https://www.cnblogs.com/artoriax/p/12612661.html
Copyright © 2011-2022 走看看