# 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 :

```                            ```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])```
```

## 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,

## 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

## 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

## 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

## Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):