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[100000],p,kk;
long long b[2][100000];
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[1][i]=a[i];
b[0][0]=0;
b[1][0]=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.InputStreamReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
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);
Task3 solver = new Task3();
solver.solve(1, in, out);
out.close();
}
}
class Task3 {
/////////////////////////////////////////////////////////////
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[2][1+sqrt];
long[][] finpdivupw= new long[2][1+sqrt];
for (int i = 1; i <=sqrt ; i++) {
finishes[0][i]=1;
if(i<sqrt)
finpdivupw[0][i]= p/i - p/(i+1);
}
finpdivupw[0][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);
}
}
class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// 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() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
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';
c = read();
}
return ret * sgn;
}
public String readLine() {
try {
return super.readLine();
} 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[100000][3],i,j,k,l,m,n,t;
long long a[1010][65000];
int main()
{
scanf("%lld %lld",&n,&t);
k = 1;
l = 0;
while(k<=t)
{
b[l][0] = k;
b[l][1] = t/(t/k);
b[l][2] = b[l][1] - b[l][0] + 1;
k = b[l][1] + 1;
l++;
}
for(i=0;i<l;i++) a[0][i] = 0;
a[0][0] = 1;
for(i=1;i<=n;i++)
{
k = 0;
for(j=0;j<l;j++)
{
k = (k+a[i-1][j]*b[j][2])%P;
a[i][l-1-j] = k;
}
}
k = 0;
for(i=0;i<l;i++) k = (k+a[n][i]*b[i][2])%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.add(i)
Pf.add(p//i)
Pf = sorted(Pf)
# print(Pf)
Ps = []
for f in Pf :
Ps.append(f)
Pfd = [0]
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()
View More Similar Problems
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 →Palindromic Subsets
Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t
View Solution →