# P-sequences

### Problem Statement :

```We call a sequence of N natural numbers (a1, a2, ..., aN) a P-sequence, if the product of any two adjacent numbers in it is not greater than P. In other words, if a sequence (a1, a2, ..., aN) is a P-sequence, then ai * ai+1 ≤ P ∀ 1 ≤ i < N

You are given N and P. Your task is to find the number of such P-sequences of N integers modulo 109+7.

Input Format

The first line of input consists of N
The second line of the input consists of P.

Constraints

2 ≤ N ≤ 103
1 ≤ P ≤ 109
1 ≤ ai

Output Format

Output the number of P-sequences of N integers modulo 109+7.```

### Solution :

```                            ```Solution in C :

In C++ :

#include<math.h>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<map>
#include<ext/hash_map>
#include<ext/hash_set>
#include<set>
#include<string>
#include<assert.h>
#include<vector>
#include<time.h>
#include<queue>
#include<deque>
#include<sstream>
#include<stack>
#include<sstream>
#define MA(a,b) ((a)>(b)?(a):(b))
#define MI(a,b) ((a)<(b)?(a):(b))
#define AB(a) (-(a)<(a)?(a):-(a))
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ep 0.0000000001
#define Pi 3.1415926535897932384626433832795
using namespace std;
using namespace __gnu_cxx;
const long long  MO=1000000000+7;
int x,y,i,j,k,n,m,mid,l,r;
int a,p,kk;
long long b;
int main()
{
cin>>n>>p;
kk=k=int(sqrt(p));
for (i=1;i<=k;i++) a[i]=i;
for (i=kk;i>=1;i--)
{
if (p/i>a[k]) {k++; a[k]=p/i;}
}
for (i=1;i<=k;i++)
b[i]=a[i];
b=0;
b=0;
for (i=2;i<=n;i++)
{
r=k;
for (j=1;j<=k;j++){
while (a[r]*a[j]>p) r--;
b[(i&1)][j]=(b[(!(i&1))][r]*(a[j]-a[j-1])+b[(i&1)][j-1])%MO;
//     cout<<b[(i&1)][j]<<" "<<a[j]<<" -- ";
}
//     cout<<endl;
}
cout<<b[(n&1)][k]<<endl;

return 0;
}

In Java :

import java.io.IOException;
import java.util.InputMismatchException;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.InputStream;

/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Nipuna Samarasekara
*/
public class Solution {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
solver.solve(1, in, out);
out.close();
}
}

/////////////////////////////////////////////////////////////
static long mod=1000000007;
public void solve(int testNumber, FastScanner in, FastPrinter out) {
long N=in.nextInt(),p=in.nextInt();
int sqrt= (int)Math.sqrt(p) ;
long[][] finishes= new long[1+sqrt];
long[][] finpdivupw= new long[1+sqrt];
for (int i = 1; i <=sqrt ; i++) {
finishes[i]=1;
if(i<sqrt)
finpdivupw[i]= p/i - p/(i+1);
}
finpdivupw[sqrt]=p/sqrt-sqrt;
int cur=0,prev=1;
for (int i = 0; i < N-1; i++) {
cur^=1;
prev^=1;
long sum=0;
for (int j = 1; j < sqrt ; j++) {
sum+=finishes[prev][j];
if(sum>=mod)sum%=mod;
long val=sum*(p/j - p/(j+1));
val%=mod;
finpdivupw[cur][j]=val;

}
sum+=finishes[prev][sqrt];
long val=  sum*(p/sqrt-sqrt);
finpdivupw[cur][sqrt]= val%mod;
for (int j = sqrt; j >= 1; j--) {
sum+=finpdivupw[prev][j];
if(sum>=mod)sum%=mod;
finishes[cur][j]=sum;
}

}
long ans=0;
for (int i = 1; i <= sqrt ; i++) {
ans+=finishes[cur][i];
if(ans>=mod)ans%=mod;
ans+=finpdivupw[cur][i];
if(ans>=mod)ans%=mod;
}
out.println(ans);
}
}

public FastScanner(InputStream is) {
}

try {
//            if (isEOF && ret < 0) {
//                throw new InputMismatchException();
//            }
//            isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}

static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}

public int nextInt() {
while (isWhiteSpace(c)) {
}
int sgn = 1;
if (c == '-') {
sgn = -1;
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
}
return ret * sgn;
}

try {
} catch (IOException e) {
return null;
}
}

}

class FastPrinter extends PrintWriter {

public FastPrinter(OutputStream out) {
super(out);
}

public FastPrinter(Writer out) {
super(out);
}

}

In C :

#include <stdio.h>

#define P 1000000007LL

long long b,i,j,k,l,m,n,t;
long long a;

int main()
{

scanf("%lld %lld",&n,&t);

k = 1;
l = 0;

while(k<=t)
{
b[l] = k;
b[l] = t/(t/k);
b[l] = b[l] - b[l] + 1;
k = b[l] + 1;
l++;
}

for(i=0;i<l;i++) a[i] = 0;
a = 1;

for(i=1;i<=n;i++)
{
k = 0;
for(j=0;j<l;j++)
{
k = (k+a[i-1][j]*b[j])%P;
a[i][l-1-j] = k;
}
}

k = 0;

for(i=0;i<l;i++) k = (k+a[n][i]*b[i])%P;

printf("%lld\n",k);

return 0;
}

In Python3 :

#!/bin/python3

import os
import sys

#
# Complete the pSequences function below.
#
def pSequences(n, p):

MOD = 10**9+7

Pf = set()
for i in range(1,int(p**0.5)+1) :
Pf = sorted(Pf)
# print(Pf)

Ps = []
for f in Pf :
Ps.append(f)

Pfd = 
for i in range(1, len(Pf)) :
Pfd.append(Pf[i]-Pf[i-1])

# print(Pfd)
for n in range(1,n):
#    print(Ps)
Ps_ = []
Ps_.append(Ps[-1])
for i in range(1,len(Pf)) :
Ps_.append((Ps_[-1] + Pfd[i] * Ps[-(i+1)]) % MOD)
Ps = Ps_

# print(Ps)
return(Ps[-1])

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

n = int(input())

p = int(input())

result = pSequences(n, p)

fptr.write(str(result) + '\n')

fptr.close()```
```

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

## Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

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