Alien Languages


Problem Statement :


Sophia has discovered several alien languages. Suprisingly, all of these languages have an alphabet, and each of them may contain thousands of characters! Also, all the words in a language have the same number of characters in it.

However, the aliens like their words to be aesthetically pleasing, which for them means that for the  ith letter of an n-letter alphabet (letters are indexed 1 . . . n ):

if 2i > n, then the ith letter may be the last letter of a word, or it may be immediately followed by any letter, including itself.

if 2i <= n, then the ith letter can not be the last letter of a word and also can only be immediately followed by jth letter if and only if j >=2i.

Sophia wants to know how many different words exist in this language. Since the result may be large, she wants to know this number, modulo 100000007(10^8 + 7).

Input Format

The first line contains t, the number of test cases. The first line is followed by t lines, each line denoting a test case. Each test case will have two space-separated integers n, m which denote the number of letters in the language and the length of words in this language respectively.

Constraints
1 <= t <= 5
1 <= n <= 10^5
1 <= m <= 5.10^5

Output Format

For each test case, output the number of possible words modulo 100000007(10^8 + 7).



Solution :



title-img


                            Solution in C :

In C++ :





#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

long long MOD = 100000007;

long long n, m, cnt[120000], d[120000], met[120000];
long long dd[1200000];

int main() {
    int t;
    cin >> t;
    for(int T = 0; T < t; ++T) {
        cin >> n >> m;
        for(int i = 1; i <= n; ++i) {
            cnt[i] = 1;
        }
        for(int q = 1; q < 30; ++q) {
            d[q] = 0;
            for(int i = 1; i <= n; ++i) {
                met[i] = 0;
                if (2 * i > n) {
                    d[q] += cnt[i];
                    d[q] %= MOD;
                    cnt[i] = 0;
                }
            }
            for(int i = 1; i <= n; ++i) {
                met[2 * i] += cnt[i];
            }
            long long now = 0;
            for(int i = 1; i <= n; ++i) {
                now += met[i];
                now %= MOD;
                cnt[i] = now;
            }
        }
        for(int i = 0; i < 1200000; ++i) {
            dd[i] = 0;
        }
        dd[0] = 1;
        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < 30; ++j) {
                dd[i + j] += (dd[i] * d[j]) % MOD;
                dd[i + j] %= MOD;
            }
        }
        cout << dd[m] % MOD << endl;
    }
    return 0;
}








In Java :





import java.util.*;

class Solution {
  static final int MOD = 100000007;
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int cases = in.nextInt();
    
    while(cases-->0) {
      int size = in.nextInt();
      int length = in.nextInt();
      int[][] array = buildArray(size);
      int[] eachSize = new int[array[0].length+1];
      for(int i=1;i<eachSize.length;++i) eachSize[i] = array[1][i-1];
      // System.err.println(Arrays.toString(eachSize));
      int[] dp = new int[length+1];
      dp[0] = 1;
      for(int i=1;i<=length;++i) {
        for(int j=1;j<eachSize.length;++j) {
          if (j > i) break;
          long temp = (long)eachSize[j];
          temp *= dp[i-j];
          temp %= MOD;
          dp[i] += temp;
          dp[i] %= MOD;
        }
      }
      System.out.println(dp[length]);
    }
  }
  
  public static int[][] buildArray(int size) {
    // size is size of alphabet
    // our array needs to be log_2(size) by size
    int depth = log2(size);
    int[][] array = new int[size+1][depth]; // letters are 1-indexed
    for(int i=size;i>0;--i) {
      // only these letters can end a word
      if (i == size) array[i][0] = 1;
      else if (i > size/2) array[i][0] = 1 + array[i+1][0];
      else array[i][0] = array[i+1][0];
    } 
    // build the rest of the array
    for(int i=size-1;i>0;--i) {
      for(int j=1;j<depth;++j) {
        if (2*i > size) {
          array[i][j] = array[i+1][j]; 
        } else {
          array[i][j] = array[i+1][j] + array[2*i][j-1];
          // if (array[i][j] < 0) System.err.println("woop");
          array[i][j] %= MOD;
        }
      }
    }
    return array;
  }
  
  // this isn't actually log2(x), since log2(1) returns 1 and not 0
  // but it's what i need!
  public static int log2(int x) {
    int ans = 0;
    while(x > 0) {
      x >>= 1;
      ++ans;
    }
    return ans;
  }
}








In C :






#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int dp[2][100001];
long long material[17];
int number[500001];

int reduce(long long value) {
    while(value < 0)
        value += 100000007;
    return value % 100000007;
}

int main() {
    int t, n, m, middle, i, j, sum, index = 0, first, second, max_length;
    long long result;
    scanf("%d", &t);
    for (; t > 0; --t) {
        scanf("%d %d", &n, &m);
        memset(dp, 0, sizeof(dp));
        memset(material, 0, sizeof(material));

        middle = n / 2;

        for (i = middle + 1; i <= n; ++i) {
            dp[index][i] = 1;
        }

        for (i = 2; i <= m + 1; ++i) {
            index = 1 - index;
            if (i == 2) {
                sum = n - middle;
            } else {
                sum = 0;
                for (j = 1; j <= middle; ++j) {
                    if (dp[1 - index][j] == 0) {
                        break;
                    }
                    sum += dp[1 - index][j];
                    sum = reduce(sum);
                }
            }
            if (sum == 0) {
                break;
            }
            material[i - 1] = sum;

            for (j = 1; j <= middle; ++j) {
                first = (j << 1) - 1;
                second = first - 1;
                sum -= dp[1 - index][first] + dp[1 - index][second];
                sum = reduce(sum);
                dp[index][j] = sum;
            }
            for (j = middle + 1; j <= n; ++j) {
                dp[index][j] = 0;
            }
        }
        max_length = i - 2;
        number[0] = 1;
        for (i = 1; i <= m; ++i) {
            result = 0;
            for (j = 1; j <= max_length && j <= i; ++j) {
                result += material[j] * number[i - j];
                result = reduce(result);
            }
            number[i] = result;
        }
        printf("%d\n", number[m]);
    }
    return 0;
}








In Python3 :






mod = 10**8 + 7

for cas in range(int(input())):
    n, m = map(int, input().strip().split())
    v = [2*i > n for i in range(n+1)]
    for i in range(n-1,-1,-1):
        v[i] += v[i + 1]
    c = []
    while v[1]:
        c.append(v[1])
        for i in range(1,n//2+1):
            v[i] = v[2*i]
        for i in range(n//2+1,n+1):
            v[i] = 0
        for i in range(n-1,-1,-1):
            v[i] = (v[i] + v[i + 1]) % mod

    f = [1] + [0]*(len(c)-1)
    for k in range(1,m+1):
        f = [sum(F * C for F, C in zip(f, c)) % mod] + f[:-1]

    print(f[0])
                        








View More Similar Problems

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →