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 :



title-img


                            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

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →