Codeforces Round #630 (Div. 2) E. Height All the Same

E. Height All the Same

outputstandard output
Alice has got addicted to a game called Sirtet recently.
In Sirtet, player is given an n×m grid. Initially ai,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.
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≤ai,j≤R for all 1≤i≤n, 1≤j≤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 (1≤n,m,L,R≤109, L≤R, n⋅m≥2).
Output
Output one integer, representing the desired answer modulo 998,244,353.
Examples
inputCopy
2 2 1 1
outputCopy
1
inputCopy
1 2 1 2
outputCopy
2
Note
In the first sample, the only initial grid that satisfies the requirements is a1,1=a2,1=a1,2=a2,2=1. Thus the answer should be 1.
In the second sample, initial grids that satisfy the requirements are a1,1=a1,2=1 and a1,1=a1,2=2. Thus the answer should be 2.

题意

给你四个整数n,m,L,R,有一个n x m的网格,你可以在网格上面通过一下两种操作 1.将一个立方体堆叠在两个相邻的单元中;
2.在一个单元格中堆叠两个立方体。初始时每个单元格中立方体大小可以在L~R之间任意。并使得上述操作使所有单元格的高度相同。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
#define  mod 998244353
using namespace std;
ll quick(ll a, ll b) 
{ 	
	ll ans=1;
	while(b)
	{
		if(b%2==1)
		ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	//printf("%lld\n",ans);
	return ans;
}
ll n,m,l,r;
int main()
{
    cin >> n >> m >> l >> r;
    ll temp=r-l+1;
    if (n * m % 2 == 1)
    {
        printf("%lld\n", quick(temp, n * m));
    } 
	else
    {
        ll ans,o,j;
        j=o=(r-l+1)/2;
        if((r-l+1)%2==1)
        {
        	if(l%2==1)j++;
        	else o++;
		}
		//printf("%d %d\n",o,j);
		quick(2,2);
		ans=quick(j+o,n*m)%mod+quick(j-o,n*m)%mod;
		//printf("%lld\n",ans);
		ans%=mod;
		ans*=quick(2,mod-2);
		ans=ans%mod;
        printf("%lld\n", ans );
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值