Xor and Sum
Problem Statement :
You are given two positive integers a and b in binary representation. You should find the following sum modulo 10^9 + 7: where operation xor means exclusive OR operation, operation shl means binary shift to the left. Please note, that we consider ideal model of binary integers. That is there is infinite number of bits in each number, and there are no disappearings (or cyclic shifts) of bits. Input Format The first line contains number a (1 <= a <2^10^5) in binary representation. The second line contains number b (1 < = b < 2^10^5) in the same format. All the numbers do not contain leading zeros. Output Format Output a single integer - the required sum modulo 10^9 + 7.
Solution :
Solution in C :
In C++ :
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
#define long long long
#define mod 1000000007ll
#define M 500500
#define N 13001000
const int T = 314159;
string a, b;
long p[M], x[M], s[N];
void pre() {
p[0] = 1;
for (int i = 1; i < M; ++i)
p[i] = (2 * p[i - 1]) % mod;
}
void read() {
cin >> a >> b;
for (int i = 0; i < (int) a.length(); ++i)
x[a.length() - 1 - i] = a[i] == '1' ? 1 : 0;
for (int i = 0; i < (int) b.length(); ++i)
s[b.length() - 1 - i + M] = b[i] == '1' ? 1 : 0;
for (int i = 1; i < N; ++i)
s[i] += s[i - 1];
}
long sum(int l, int r) {
return s[r + M] - s[l + M];
}
void kill() {
long ans = 0;
for (int i = 0; i < M; ++i)
if (x[i] == 0)
ans = (ans + p[i] * sum(i - T - 1, i)) % mod;
else
ans = (ans + p[i] * (T + 1 - sum(i - T - 1, i))) % mod;
cout << ans << "\n";
}
int main() {
#ifdef TROLL
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#else
ios_base::sync_with_stdio(0);
#endif
pre();
read();
kill();
return 0;
}
In Java :
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
final int n = 314159;
final int maxlen = 1000000;
final int mod = 1000000007;
Scanner in = new Scanner(System.in);
char a[] = in.nextLine().toCharArray();
char b[] = in.nextLine().toCharArray();
int bina[] = new int[maxlen];
int binb[] = new int[maxlen];
int bone[] = new int[maxlen];
for(int i=0;i<a.length;i++){
bina[i]=a[a.length-1-i]-'0';
}
for(int i=a.length;i<maxlen;i++){
bina[i]=0;
}
for(int i=0;i<b.length;i++){
binb[i]=b[b.length-1-i]-'0';
}
for(int i=b.length;i<maxlen;i++){
binb[i]=0;
}
bone[0]=binb[0];
for(int i = 1;i <= n;i++){
bone[i]=bone[i-1]+binb[i];
}
for(int i=n+1;i<1000000;i++){
bone[i]=bone[i-1]+binb[i]-binb[i-n-1];
}
long sum = 0;
long mul = 1;
for(int i=0;i<maxlen;i++){
if(bina[i]==1){
sum = (sum + (mul*(n+1-bone[i]))%mod)%mod;
} else if(bina[i]==0){
sum = (sum + (mul*(bone[i]))%mod)%mod;
}
mul=(mul*2)%mod;
}
System.out.println(sum);
}
}
In C :
#include <stdio.h>
#include <stdlib.h>
#define MOD 1000000007
#define s(k) scanf("%d",&k);
#define sll(k) scanf("%lld",&k);
#define p(k) printf("%d\n",k);
#define pll(k) printf("%lld\n",k);
#define f(i,N) for(i=0;i<N;i++)
#define f1(i,N) for(i=0;i<=N;i++)
#define f2(i,N) for(i=1;i<=N;i++)
#define lim 314160
typedef long long ll;
void rev(char* in){
int start=0,end=strlen(in)-1,i,l=strlen(in);
char t;
while(start<end){
t=in[start];
in[start]=in[end];
in[end]=t;
start++;
end--;
}
for(i=strlen(in);i<lim+l;i++)
in[i]='0';
}
int main(){
char A[414160],B[414160];
int i,num,len;
ll X,sum,pos;
scanf("%s",A);
scanf("%s",B);
len=strlen(B);
rev(A);
rev(B);
num=0;
pos=1;
sum=0;
for(i=0;i<lim-1;i++){
if(B[i]=='1')
num++;
X=num;
if(A[i]=='1')
X=lim-X;
sum=(sum+X*pos)%MOD;
pos=(pos<<1)%MOD;
}
for(i=0;i<len;i++){
X=num;
sum=(sum+X*pos)%MOD;
pos=(pos<<1)%MOD;
if(B[i]=='1')
num--;
}
pll(sum);
/* printf("%s\n",A);
printf("%s\n",B);*/
return 0;
}
In Python3 :
a = int( input( ), 2 )
b = int( input( ), 2 )
r = 0
for i in range( 314160 ):
r += a ^ b
b *= 2
print( (r % (10**9+7)) )
View More Similar Problems
Game of Two Stacks
Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f
View Solution →Largest Rectangle
Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle
View Solution →Simple Text Editor
In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,
View Solution →Poisonous Plants
There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan
View Solution →AND xor OR
Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value
View Solution →Waiter
You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the
View Solution →