Problem Description
In the last semester, Rikka joined the badminton club.
There are n students in the badminton club, some of them have rackets, and some of them have balls. Formally, there are a students have neither rackets nor balls, b students have only rackets, c students have only balls, and d students have both rackets and balls. (a+b+c+d=n)
This week, the club is going to organize students to play badminton. Each student can choose to take part in or not freely. So there are 2n possible registration status.
To play badminton, there must be at least two students who have rackets and at least one students who have balls. So if there aren't enough balls or rackets, the activity will fail.
Now, Rikka wants to calculate the number of the status among all 2n possible registration status which will make the activity fail.
Input
The first line contains a single number t(1≤t≤103), the number of testcases.
For each testcase, the first line contains four integers a,b,c,d(0≤a,b,c,d≤107,a+b+c+d≥1).
Output
For each testcase, output a single line with a single integer, the answer modulo 998244353.
Sample Input
3 1 1 1 1 2 2 2 2 3 4 5 6
Sample Output
12 84 2904
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
const int mod= 998244353;
ll pow_mod(ll base,int n,int mod){
ll ans=1;
while(n){
if(n&1)ans=ans*base%mod;
base=base*base%mod;
n>>=1;
}
return ans%mod;
}
ll solve(int a,int b){
if(b==0)return pow_mod(2,a,mod);
else{
if(b>a) return 0ll;
ll sum=pow_mod(2,a,mod);
sum=sum-1;
if(sum<0)sum+=mod;
// printf("a:%d b:%d sum:%lld\n",a,b,sum);
if(b==1) return sum;
sum=sum-a;
if(sum<0)sum+=mod;
return sum;
}
}
/*
容斥原理
考虑成功的一共有3中情况
A: b>=2&&c>=1
B: d>=2
C: b>=1&&d>=1
所以成功的结果就是 |A并B并C|,
然后从总情况中减掉就好了
*/
ll ans[10];
int main(){
int T;
scanf("%d",&T);
while(T--){
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
int n=a+b+c+d;
ll sum=pow_mod(2,n,mod);
ans[1]=solve(a,0)*solve(b,2)%mod*solve(c,1)%mod*solve(d,0)%mod;
ans[2]=solve(a,0)*solve(b,0)%mod*solve(c,0)%mod*solve(d,2)%mod;
ans[3]=solve(a,0)*solve(b,1)%mod*solve(c,0)%mod*solve(d,1)%mod;
ans[4]=solve(a,0)*solve(b,2)%mod*solve(c,1)%mod*solve(d,2)%mod;
ans[5]=solve(a,0)*solve(b,2)%mod*solve(c,1)%mod*solve(d,1)%mod;
ans[6]=solve(a,0)*solve(b,1)%mod*solve(c,0)%mod*solve(d,2)%mod;
ans[7]=solve(a,0)*solve(b,2)%mod*solve(c,1)%mod*solve(d,2)%mod;
for(int i=1;i<=3;i++){
sum=sum-ans[i];
if(sum<0)sum+=mod;
}
for(int i=4;i<=6;i++){
sum=sum+ans[i];
if(sum>=mod)sum-=mod;
}
sum=(sum-ans[7]+mod)%mod;
// rep(i,1,8)printf("%lld%c",ans[i],i==7?'\n':' ');
printf("%lld\n",sum);
}
return 0;
}