列表

详情


NC245411. Exercise In Class

描述

In the new semester, TTL and YXH learn two new functions in class: the fifirst function f(x) is defifined as the number of ‘1’ in the binary representation of x, for example, f(1) = 1,f(3) = 2,f(5) = 2; The second function g(x) = f(x & (x >> 1)), where ‘ & ’ denotes bitwise AND, and ‘ >> ’ denotes bitwise right
shift operation.
To test how far they grasp the two functions, their teacher assigns a class exercise. In this exercise, an integer k is given, then both TTL and YXH need to choose an interval respectively, denoted as [l1, r1] and [l2, r2]. After that, the teacher asks them to fifind all pairs (x, y) such that x ∈ [l1, r1], y ∈ [l2, r2], and x ⊕ y ≤ k, then calculate the sum of g(x|y)! , where ‘ ⊕ ’ denotes bitwise XOR, ‘ | ’ denotes bitwise OR, and ‘ ! ’ is the factorial notation. If there is no such pair, the answer is 0.
TTL and YXH think the problem is too difficult, so they ask smart you for help. Given the integer k and the two intervals [l1, r1] and [l2, r2], can you help them fifigure out the answer?
Since the answer is likely to be very large, you just have to fifigure out the answer modulo 998244353.

输入描述

The fifirst line contains an integer T(1 ≤ T ≤ 103 ), denoting the number of test cases.
The only line of each test case contains fifive integers l1, r1, l2, r2, k (1 ≤ l1 ≤ r1 ≤ 109 , 1 ≤ l2 ≤ r2 ≤ 109 , 0 ≤ k ≤ 109 ) separated by space.

输出描述

For each test case, output one line with an integer, indicating the answer modulo 998244353.

示例1

输入:

3
1 3 1 3 2
2 2 3 3 0
1 14 5 14 114514

输出:

7
0
396

说明:

For the fifirst test case, there are 7 possible pairs:
(1, 1),(1, 3),(2, 2),(2, 3),(3, 1),(3, 2),(3, 3).
So the answer is: 0! + 1! + 0! + 1! + 1! + 1! + 1! = 7.
For the second test case, there isn’t any possible pair, so the answer is 0.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 293ms, 内存消耗: 808K, 提交时间: 2023-05-09 23:14:17

#include <bits/stdc++.h>
using namespace std;

const int MOD = 998244353;

int K;
int len1, bit1l[32], bit1r[32], len2, bit2l[32], bit2r[32];
int A, B, C, D, fact[32];
int f[32][2][2][2][2][2][32][2];
// pos a b c d le sum-block last

int dfs(int pos, bool la, bool lb, bool lc, bool ld, bool le, int t, bool last) {
	if (pos < 0)	return fact[t];
	if (f[pos][la][lb][lc][ld][le][t][last] != -1)	return	f[pos][la][lb][lc][ld][le][t][last];
	int a = la ? (A >> pos & 1) : 0;
	int b = lb ? (B >> pos & 1) : 0;
	int c = lc ? (C >> pos & 1) : 1;
	int d = ld ? (D >> pos & 1): 1;
	
	long long res = 0;
	for (int x = a; x <= c; x ++)
		for (int y = b; y <= d; y ++) {
			int u = x ^ y, v = K >> pos & 1;
			int w = x | y;
			if (u < v) {
				res += dfs(pos - 1, la && x == a, lb && y == b, lc && x == c, ld && y == d, true, t + (w == 1 and last == 1), w);
			} else if (u == v) {
				res += dfs(pos - 1, la && x == a, lb && y == b, lc && x == c, ld && y == d, le, t + (w == 1 and last == 1), w);
			} else {  // u > v
				if (le == false)	continue;
				res += dfs(pos - 1, la && x == a, lb && y == b, lc && x == c, ld && y == d, le, t + (w == 1 and last == 1), w);
			}
			res %= MOD;
		}
	return f[pos][la][lb][lc][ld][le][t][last] = res;
}

int main() {
	int T; cin >> T;
	fact[0] = 1;
	for (int i = 1; i < 32; i ++)	fact[i] = 1ll * i * fact[i - 1] % MOD;
	while (T --) {
		memset(f, -1, sizeof f);
		cin >> A >> C >> B >> D >> K;
		
		cout << dfs(30, 1, 1, 1, 1, 0, 0, 0) << '\n';
	}
	return 0;
}

C++(clang++ 11.0.1) 解法, 执行用时: 458ms, 内存消耗: 640K, 提交时间: 2022-11-09 19:37:19

#include<bits/stdc++.h>
using namespace std;
const int N=39,mod=998244353;
int T,l1,r1,l2,r2,k,ans;
int fac[N],f[N][N][2][2][2][2];
int dfs(int x,int y,int ltx,int lty,int ltk,int b,int cnt,int pre)
{
	if(b==-1) return fac[cnt];
	int &res=f[b][cnt][pre][ltx][lty][ltk];
	if(res) return res;
	int tx=(ltx?(x>>b&1):1),ty=(lty?(y>>b&1):1);
	for(int i=0,t;i<=tx;i++)
		for(int j=0;j<=ty;j++)
		{
			if(ltk&&(i^j)>(k>>b&1)) continue;
			if(pre&&(i|j)) t=1;
			else t=0;
			(res+=dfs(x,y,ltx&(i==tx),lty&(j==ty),ltk&((i^j)==(k>>b&1)),b-1,cnt+t,i|j))%=mod;
		}
	return res;
}
void solve()
{
	cin>>l1>>r1>>l2>>r2>>k;
	ans=0;
	memset(f,0,sizeof(f));
	ans+=dfs(r1,r2,1,1,1,30,0,0); ans%=mod;
	memset(f,0,sizeof(f));
	ans-=dfs(l1-1,r2,1,1,1,30,0,0); (ans+=mod)%=mod;
	memset(f,0,sizeof(f));
	ans-=dfs(r1,l2-1,1,1,1,30,0,0); (ans+=mod)%=mod;
	memset(f,0,sizeof(f));
	ans+=dfs(l1-1,l2-1,1,1,1,30,0,0); ans%=mod;
	cout<<ans<<"\n";
}
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	fac[0]=1;
	for(int i=1;i<=33;i++)
		fac[i]=1ll*fac[i-1]*i%mod;
	cin>>T;
	while(T--) solve();
	return 0;
}

上一题