Ones and Twos


Problem Statement :


You are using at most A number of 1s and at most B number of 2s. How many different evaluation results are possible when they are formed in an expression containing only addition + sign and multiplication * sign are allowed?

Note that, multiplication takes precedence over addition.

For example, if A=2 and B=2, then we have the following expressions:

1, 1*1 = 1
2, 1*2, 1*1*2, 1+1 = 2
1+2, 1+1*2 = 3
2+2, 2*2, 1+1+2, 1*2*2, 1*1*2*2, 1*2+1*2, 1*1*2+2, 1*2+2 = 4
1+2+2, 1+1*2+2 = 5
1+1+2+2, 1+1+2*2 = 6
So there are 6 unique results that can be formed if A = 2 and B = 2.

Input Format

The first line contains the number of test cases T, T testcases follow each in a newline.
Each testcase contains 2 integers A and B separated by a single space.

Constraints

1 <= T <= 105
0<=A<=1000000000
0<=B<=1000

Output Format

Print the number of different evaluations modulo (%) (109+7.)



Solution :



title-img


                            Solution in C :

In C++ :





#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int MOD = 1e9 + 7;
int dp_sum[1001][1001],dp_bit[1001][1001];
void add(int &x,long long v){
    v%=MOD;
    x+=v;
    if(x>=MOD)x-=MOD;
}
int f_bit(int lv,int B);
int f_sum(int lv,int B){
    if(lv>B)return 1;
    if(dp_sum[lv][B]!=-1)return dp_sum[lv][B];
    int tmp=f_sum(lv+1,B);
    add(tmp,f_bit(lv,B));
    return dp_sum[lv][B]=tmp;

}
int f_bit(int lv,int B){
    if(lv>B)return 0;
    if(dp_bit[lv][B]!=-1)return dp_bit[lv][B];
    return dp_bit[lv][B]=f_sum(lv+1,B-lv);
}
int main(){
    int T,A,B;
    scanf("%d",&T);
    memset(dp_sum,-1,sizeof(dp_sum));
    memset(dp_bit,-1,sizeof(dp_bit));
    while(T--){
        scanf("%d%d",&A,&B);
        int an=0;
        if(A==0)an=f_sum(1,B);
        else{
            int k=1;
            while((1LL<<k)<=A)k++;
            k++;
            add(an,(long long)(A+1)*f_sum(k,B));
            long long ha=(1LL<<k)-A-1;
            int now=0,i=1;
            long long last=0;
            while(ha>0){
                now++;
                if(B<now)break;
                add(an,min((1LL<<i)-last,ha)*f_sum(k,B-now));
                ha-=(1LL<<i)-last;
                last=1LL<<i;
                i++;
                if(i==k){
                    last=0;
                    i=1;
                }
            }
            /*
            long long last=A;
            for(int i=1;i<=B;i++){
                if(last+1>=(1LL<<k))break;
                add(an,(min(((long long)A+(1LL<<i)),(1LL<<k)-1)-last)*f_sum(k,B-i));
                last=min((long long)A+(1LL<<i),(1LL<<k)-1);
            }*/
        }
        printf("%d\n",(an+MOD-1)%MOD);
    }
    return 0;
}








In Java :





import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Solution {

 static Solution main;

 public class Range implements Comparable<Range> {
  int minPower;
  int maxPower;
  long range;
  public Range(int min , int max , long r) {
   minPower = min;
   maxPower = max;
   range = r;
  }
  public int compareTo(Range r) {
   int comp = new Integer(minPower).compareTo(r.minPower);
   if(comp!=0) {
    return comp;
   }
   return  new Integer(maxPower).compareTo(r.maxPower);
  }
 }

 public static void main(String[] args) {
  main = new Solution();
  long [][] dp = new long[1001][1001];
  long [][] dp1 = new long[1001][1001];
  long [][] dpsum = new long[1001][1001];
  long sp = 1000000007;
  for(int i = 0 ; i < 1001 ; i++) {
   Arrays.fill(dp[i], 0);
  }
  long [] pow2 = new long [32];
  pow2[0] = 1;
  for(int i = 1 ; i <32 ; i++) {
   pow2[i ] = pow2[i-1] * 2;
  }
  dp[1][1] = 1;
  for(int i = 2 ; i < 1001 ; i++) {
   for(int j = 1 ; j <= i ; j++) {
    long temp = 0;
    for(int k = j+1 ; k < i ; k++) {
     temp += dp[i-j][k];
     if(temp>=sp) {
      temp-=sp;
     }
    }
    if(i==j) {
     temp++;
     if(temp>=sp) {
      temp-=sp;
     }
    }
    dp[i][j] = temp ;
   }
  }
  for(int k = 0 ; k < 1001 ; k++) {
   Arrays.fill(dp1[k],0);
  }
  for(int i = 0 ; i < 1001 ; i++) {
   dpsum[i][0] = 0;
  }
  for(int i = 0 ; i < 1001 ; i++) {
   for(int j = 1 ; j < 1001 ; j++) {
    dpsum[i][j] = dpsum[i][j-1] + dp[i][j];
    if(dpsum[i][j]>=sp) {
     dpsum[i][j] -= sp;
    }
   }
  }
  for(int k = 1 ; k < 1001 ; k++) {
   for(int i = k ; i < 1001 ; i++) {
    if(i==k) {
     dp1[i][k] = 1;
    } else {
     dp1[i][k] = dp1[i-1][k] + dp[i][k];
     if(dp1[i][k] >= sp) {
      dp1[i][k] -= sp;
     }
    }
   }
  }
  long [][][] list = new long[1001][32][32];
  long [] all = new long[1001];
  for(int i = 0 ; i < 1001 ; i++) {
   for(int j = 0 ; j < 32 ; j++) {
    for(int k = 0 ; k < 32 ; k++) {
     list[i][j][k] = 0;
    }
   }
   all[i] = 0;
   for(int j = 1 ; j <= Math.min(i/2, 500) ; j++) {
    for(int k = j + 1 ; k <= i-j ; k++) {
     long repValue = 0;
     if(j+k==i) {
      repValue ++;
     }
     repValue += dpsum[i-j-k][i-j-k] - dpsum[i-j-k][k];
     if(repValue<0) {
      repValue += sp;
     }
     if(repValue>=sp) {
      repValue -= sp;
     }
     if(k<32) {
      list[i][j][k] = repValue;
     } else {
      all[i] += repValue;
     }
    }
   }
   all[i] %= sp;
  }
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  BufferedOutputStream bos = new BufferedOutputStream(System.out);
  String eol = System.getProperty("line.separator");
  byte[] eolb = eol.getBytes();
  try {
   String str = br.readLine();
   int t = Integer.parseInt(str);
   for(int i = 0 ; i < t ; i++) {
    str = br.readLine();
    int blank = str.indexOf(" ");
    int a = Integer.parseInt(str.substring(0,blank));
    int b = Integer.parseInt(str.substring(blank+1));
    long ans = 0;
    if(b==0) {
     bos.write(new Long(a).toString().getBytes());
    }else if (a==0){
     for(int d = 0 ; d <= b ; d++) {
      long temp = dp1[b][d];
      ans += temp; 
     }
     ans %= sp;
     bos.write(new Long(ans).toString().getBytes());
    } else {
     for(int d = 0 ; d < b ; d++) {
      long temp = dp1[b-1][d];
      long mult = 2;
      temp *= mult;
      ans += temp; 
     }
     ans %= sp;
     for(int d = 0 ; d < 32 ; d++) {  
      for(int e = d+1 ; e < 32 ; e++ ) {
       long repValue = list[b][d][e];
       long temp = repValue * Math.min(a+1,pow2[e]-pow2[d]);
       ans += temp;
       ans %= sp;
      }
     }
     ans %= sp;
     long temp = all[b] * (a+1);
     ans += temp;
     ans+=a+2;
     ans %= sp;
     bos.write(new Long(ans).toString().getBytes());
    }
    bos.write(eolb);
   }
   bos.flush();
  } catch(IOException ioe) {
   ioe.printStackTrace();
  }
 }
}









In C :





#include <stdio.h>
#include <stdlib.h>
#define MOD 1000000007
long long dp[1001][1001][31]={0},dp3[1001];

int main(){
  int T,A,B,l,i,j,k;
  long long t,f;
  for(k=0;k<31;k++)
    for(i=k+1;i<=1000;i++)
      for(j=k+1;j<=1000;j++)
        if(i==k+1){
          if(j==k+1)
            dp[i][j][k]=1;
        }
        else{
          dp[i][j][k]=dp[i-1][j][k];
          if(j>i)
            dp[i][j][k]=(dp[i][j][k]+dp[i-1][j-i][k])%MOD;
          if(i==j)
            dp[i][j][k]=(dp[i][j][k]+1)%MOD;
        }
  scanf("%d",&T);
  while(T--){
    scanf("%d%d",&A,&B);
    i=A;
    for(k=0;i;i>>=1,k++);
    for(i=0,t=1;i<k+1;i++,t<<=1);
    dp3[0]=(A+1)%MOD;
    l=1;
    for(i=f=1,j=2;i<=k && i<=B;i++,j<<=1,l=i)
      if(j+A<t)
        dp3[i]=(j+A+1)%MOD;
      else{
        dp3[i]=t%MOD;
        f=0;
        l=i+1;
        break;
      }
    if(f){
      f=((j>>1)&0x7fffffff);
      for(i=1,j=2;i<=k-1 && i+k<=B;i++,j<<=1,l=i+k)
        if(j+f+A<t)
          dp3[i+k]=(j+f+A+1)%MOD;
        else{
          dp3[i+k]=t%MOD;
          l=i+k+1;
          break;
        }
    }
    for(;l<1001;l++)
      dp3[l]=dp3[l-1];
    for(i=k+1,t=dp3[1000];i<=B;i++)
      t=(t+dp[B][i][k]*dp3[B-i])%MOD;
    printf("%lld\n",(t-1+MOD)%MOD);
  }
  return 0;
}
                        








View More Similar Problems

Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →

The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →

Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →

Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →