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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章