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

Array Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

View Solution →

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →

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 →