B. Divan and a New Project
还是那个问题:
题目很简单,但是注意一点,类型转换的问题
Wrong answer on pretest 4:sum+=abs(bb)*2*(a[i].d);
Accepted:sum+=1ll*abs(bb)*2*(a[i].d);
C. Divan and bitwise operations (组合数学&位运算)
跟这题可以说是一样的 P5390 [Cnoi2019] 数学作业 (位运算&组合数学)
P5390 [Cnoi2019] 数学作业
题目描述
现在 Cirno 手上有着 T 天的作业,每天的作业可以用一个二元组 ( n, V ) 表示,其中 nn 表示集合的大小, V 表示大小为 n的集合. 现在,Cirno 需要求出的是 V 的所有子集的异或和的和,答案对 998,244,353998244353 取模。
这位运算看了老半天了……
题意:是求一个集合中所有子集异或和之和。
因为是异或运算,所以先拆位。考虑每一位,可以想到,要使贡献为 11,那么肯定是选奇数个 1 和偶数个 0。
我们分两种情况看一下:
所有的 n个数此位都是 0。那么显然贡献是 0。
有至少一个数的此位是 1。那么考虑其他 n−1 个数不管选或者不选,都只需要控制这个 1 的选或不选,就可以满足。所以情况是 2(n-1)。
P5390 code:
#include <bits/stdc++.h>
#define N 3000005
#define mod 998244353
#define ll long long
using namespace std;
ll T, arr[N];
inline ll work(ll x, ll y) {
ll r = 1;
while (y > 0) {
if (y & 1)
r = r * x % mod;
x = x * x % mod;
y >>= 1;
}
return r;
}
int main() {
cin>>T;
for (ll k=1; k<=T; k++ ) {
ll n, ans = 0;
cin>>n;
for (ll i=1; i<=n; i++ ) {
cin>>arr[i];
ans |= arr[i];
}
cout << (ans * work(2, n - 1) % mod) << endl;
}
return 0;
}
C题的code:
#include<iostream>
using namespace std;
int a[200010],mod=1e9+7;
int main()
{
int t,n,m,l,r,x;
cin>>t;
while(t--)
{
cin>>n>>m;
long long res=0;
while(m--)cin>>l>>r>>x,res|=x;
n--;
res%=mod;
while(n--)res=(res*2)%mod;
cout<<res<<endl;
}
return 0;
}