Submission #1126232

#TimeUsernameProblemLanguageResultExecution timeMemory
1126232InvMODIntergalactic ship (IZhO19_xorsum)C++20
83 / 100
401 ms12100 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define gcd __gcd #define sz(v) (int) v.size() #define pb push_back #define pi pair<int,int> #define all(v) (v).begin(), (v).end() #define compact(v) (v).erase(unique(all(v)), (v).end()) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define dbg(x) "[" #x " = " << (x) << "]" ///#define int long long using ll = long long; using ld = long double; using ull = unsigned long long; template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;} template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;} const int N = 1e3 + 1; const int QN = 1e5 + 1; const int MOD = 1e9 + 7; // modulo int add(int x, int y){ return (x + y < MOD ? x + y : x + y - MOD); } int sub(int x, int y){ return add(x - y, MOD); } int mul(int x, int y){ return (1ll * x * y) % MOD; } int bpow(int x, int y){ int res = 1; while(y){ if(y&1) res = mul(res, x); x = mul(x, x); y >>= 1; } return res; } const int lg = 7; int n, q, a[N], pw2[QN], ql[QN], qr[QN], qx[QN], save_case[3][N][N]; // Cach de chon so luong le hoac chan thang trong x thang // Lay san 1 thak nhung thak con lai tu sinh tu diet :D int comb(int x, int type){ // Luon co 1 cach cho chan if(!x) return (type ^ 1); assert(pw2[x - 1] > 0); return pw2[x - 1]; } void update_data(int _case, int bt1, int bt2, int x1, int y1, int x2, int y2){ save_case[_case][x1][y1]++; save_case[_case][x1][y2 + 1]--; save_case[_case][x2 + 1][y1]--; save_case[_case][x2 + 1][y2 + 1]++; return; } void preprocess(){ for(int _case = 0; _case < 3; _case++){ //cout << "CASE: " << _case <<"\n"; // cout << dbg(bt1) << dbg(bt2) <<"\n"; for(int l = 1; l <= n; l++){ for(int r = 1; r <= n; r++){ // Prefix sum: a[x][y] = a[x][y] + a[x - 1][y] + a[x][y - 1] - a[x - 1][y - 1] //int dbg_val = save_case[_case][bt1][bt2][l][r]; // cout << "Pre: " << dbg(l) << dbg(r) << dbg(dbg_val) <<"\n"; save_case[_case][l][r] += save_case[_case][l][r - 1] + save_case[_case][l - 1][r] - save_case[_case][l - 1][r - 1]; //dbg_val = save_case[_case][l][r]; // cout << "Do: " << dbg(l) << dbg(r) << dbg(dbg_val) <<"\n"; // assert(dbg_val >= 0); } // cout <<"\n"; //cout <<"\n"; } //cout <<"\n"; } return; } void solve() { cin >> n; for(int i = 1; i <= n; i++){ cin >> a[i]; } cin >> q; for(int i = 1; i <= q; i++){ cin >> ql[i] >> qr[i] >> qx[i]; } pw2[0] = 1; for(int i = 1; i <= q; i++) pw2[i] = mul(pw2[i -1], 2); int answer = 0; // Xet 2 bit for(int bt1 = 0; bt1 < lg; bt1++){ for(int bt2 = 0; bt2 < lg; bt2++){ for(int _case = 0; _case < 3; _case++){ for(int i = 0; i <= n; i++){ for(int j = 0; j <= n; j++){ save_case[_case][i][j] = 0; } } } for(int i = 1; i <= q; i++){ // Fix mot thak random va so sanh voi mot thak random khac va 2 bit bt1, bt2 -> // co 3 case anh huong khi query update: (0, 1) | (1, 0) | (1, 1) int l = ql[i], r = qr[i], x = qx[i]; if((x >> bt1 & 1) && (x >> bt2 & 1)){ // (i, L) -> (i, R) (L <= i <= R) thi se la case 2: (1, 1) update_data(2, bt1, bt2, l, l, r, r); // (i, L) -> (i, R) (1 <= i < L) thi se la case 0: (0, 1) update_data(0, bt1, bt2, 1, l, l-1, r); // (L, i) -> (R, i) (R+1 <= i <= n) thi se la case 1: (1, 0) update_data(1, bt1, bt2, l, r+1, r, n); } // Huhuhuhuhuhuhuhuhuhuhuuhuhuhuhuhuhuhuhuhuhuhuhuhu sai xam l else if((x >> bt1 & 1)){ // do ko update bt2 nen kieu j cx la (1, 0) (case 1) // nhx thak (L,R) -> (L,n) update_data(1, bt1, bt2, l, l, r, n); } else if((x >> bt2 & 1)){ // tuong tu bt1 // nhx thak (1,R) -> (L,R) update_data(0, bt1, bt2, 1, l, r, r); } } preprocess(); // Xet 2 thak random for(int i = 1; i <= n; i++){ for(int j = i; j <= n; j++){ // So cach chon 2 thak i,j int bta = (a[i] >> bt1 & 1); int btb = (a[j] >> bt2 & 1); int sum_case = 0; for(int case0 = 0; case0 < 2; case0++){ for(int case1 = 0; case1 < 2; case1++){ for(int case2 = 0; case2 < 2; case2++){ int nbta = (bta + case1 + case2) & 1; int nbtb = (btb + case0 + case2) & 1; assert(nbta == 1 || nbta == 0); assert(nbtb == 1 || nbtb == 0); if(nbta > 0 && nbtb > 0){ // So cach chon case0 * case1 * case2 sum_case = add(sum_case, mul(comb(save_case[0][i][j], case0), mul(comb(save_case[1][i][j], case1), comb(save_case[2][i][j], case2) ))); } } } } // Nhung thak (0, 0) con lai chon sao cung dc int left = q - save_case[0][i][j] - save_case[1][i][j] - save_case[2][i][j]; assert(left >= 0); sum_case = mul(sum_case, pw2[left]); // i = j -> 1 else 2 int tot = (i == j ? 1 : 2); tot = mul(tot, (1 << bt1)); tot = mul(tot, (1 << bt2)); assert(tot > 0); int choosePairs = mul(i, n - j + 1); int contrib = mul(tot, mul(sum_case, choosePairs)); /* if(contrib) cout << dbg(i) << dbg(j) << dbg(bt1) << dbg(bt2) << dbg(tot) << dbg(sum_case) << dbg(contrib) << "\n"; if(i == 1 && j == 1 && bt1 == 1 && bt2 == 0){ cout << "DUMB: " << contrib <<" " << left <<" " << sum_case <<" " << choosePairs <<" " << tot <<"\n"; } */ answer = add(answer, contrib); } } } } cout << answer <<"\n"; return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "InvMOD" if(fopen(name".INP", "r")){ freopen(name".INP","r",stdin); freopen(name".OUT","w",stdout); } int t = 1; //cin >> t; while(t--) solve(); return 0; } /* Debug Test 2 2 1 1 1 1 1 Answer: 40 2 1 2 1 2 2 2 Answer: 16 */

Compilation message (stderr)

xorsum.cpp: In function 'int main()':
xorsum.cpp:222:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  222 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
xorsum.cpp:223:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  223 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...