Alice and Bob are playing a game to get rid of the boredom during this lockdown. The game consists of N rounds.
Alice challenges Bob in each round of the game by asking for the output of the following loop using the integers A, B, C, and D
sum = 0;
for(i=A;i<=B;i++) {
for(j=C;j<=D;j++) {
sum = sum + (i^j)
}
}
print(sum)
Note: Here ^ stands for Bitwise XOR and other symbols have their usual meanings.
For Bob to win the game, he needs to answer Alice's questions quickly. He needs your help to do this.
Example:
Input: a[] = { 1, 2, 3, 4 }
Output: 14
Approach
Java
public class LoopProblem {static int mod = (int) 1e9 + 7;public static void main(String[] args) throws Exception {int[] a = { 1, 2, 3, 4 };long ans = 0;for (int i = 0; i < 30; i++) {long cur1 = solve(i, a[1]) - solve(i, a[0] - 1);long cur2 = solve(i, a[3]) - solve(i, a[2] - 1);long temp = (cur1 * (a[3] - a[2] + 1 -cur2) % mod * (1 << i)) % mod;temp = temp + (cur2 * (a[1] - a[0] + 1 -cur1) % mod * (1 << i)) % mod;temp %= mod;ans = (ans + temp) % mod;}System.out.println(ans);}static long solve(int i, int b) {return 1l * ((b + 1) / (1 << (i + 1))) * (1 << i)+ (((b + 1) % (1 << (i + 1))) -(1 << i) < 0 ? 0 : ((b + 1) % (1 << (i + 1))) - (1 << i));}}
C++
#include <bits/stdc++.h>using namespace std;int mod = 1e9 + 7;long solve(int i, int b){return 1l * ((b + 1) / (1 << (i + 1))) * (1 << i) +(((b + 1) % (1 << (i + 1))) -(1 << i) <0? 0: ((b + 1) % (1 << (i + 1))) - (1 << i));}int main(){vector<int> a = {1, 2, 3, 4};long ans = 0;for (int i = 0; i < 30; i++){long cur1 = solve(i, a[1]) - solve(i, a[0] - 1);long cur2 = solve(i, a[3]) - solve(i, a[2] - 1);long temp = (cur1 * (a[3] - a[2] + 1 - cur2) % mod *(1 << i)) %mod;temp = temp + (cur2 * (a[1] - a[0] + 1 - cur1) %mod * (1 << i)) %mod;temp %= mod;ans = (ans + temp) % mod;}cout << ans << "\n";}
No comments:
Post a Comment