mirror of
https://github.com/Hizenberg469/Algorithms-snippets.git
synced 2026-04-19 14:42:23 +03:00
Binomial Coefficient
This commit is contained in:
@@ -1,183 +1,183 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
//used for ignoring/removing the assert statement at compile-time.
|
//used for ignoring/removing the assert statement at compile-time.
|
||||||
//#define NDEBUG
|
//#define NDEBUG
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Edge{
|
class Edge{
|
||||||
public:
|
public:
|
||||||
int from, to;
|
int from, to;
|
||||||
Edge* residual;
|
Edge* residual;
|
||||||
long flow;
|
long flow;
|
||||||
long capacity;
|
long capacity;
|
||||||
|
|
||||||
Edge(int from, int to, long capacity){
|
Edge(int from, int to, long capacity){
|
||||||
this->from = from;
|
this->from = from;
|
||||||
this->to = to;
|
this->to = to;
|
||||||
this->capacity = capacity;
|
this->capacity = capacity;
|
||||||
flow = 0;
|
flow = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isResidual(){
|
bool isResidual(){
|
||||||
return capacity == 0;
|
return capacity == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
long remainingCapacity(){
|
long remainingCapacity(){
|
||||||
return capacity - flow;
|
return capacity - flow;
|
||||||
}
|
}
|
||||||
|
|
||||||
void augment(long bottleNeck){
|
void augment(long bottleNeck){
|
||||||
flow += bottleNeck;
|
flow += bottleNeck;
|
||||||
residual->flow -= bottleNeck;
|
residual->flow -= bottleNeck;
|
||||||
}
|
}
|
||||||
|
|
||||||
string toString(int s ,int t){
|
string toString(int s ,int t){
|
||||||
string u = (from == s) ? "s" : ((from == t) ? "t" : to_string(from));
|
string u = (from == s) ? "s" : ((from == t) ? "t" : to_string(from));
|
||||||
string v = (to == s ) ? "s" : ((to == t) ? "t" : to_string(to));
|
string v = (to == s ) ? "s" : ((to == t) ? "t" : to_string(to));
|
||||||
string res = "Edge " + u + " -> " + v + " | " + " flow = " +
|
string res = "Edge " + u + " -> " + v + " | " + " flow = " +
|
||||||
to_string(flow) + " | " + " capacity: " + to_string(capacity)
|
to_string(flow) + " | " + " capacity: " + to_string(capacity)
|
||||||
+ " | " + " is residual: " + to_string((int)isResidual()) ;
|
+ " | " + " is residual: " + to_string((int)isResidual()) ;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class NetworkFlowSolverBase{
|
class NetworkFlowSolverBase{
|
||||||
protected :
|
protected :
|
||||||
long INF = INT_MAX/2;
|
long INF = INT_MAX/2;
|
||||||
//Input: n = Number of nodes, s = source, t = sink
|
//Input: n = Number of nodes, s = source, t = sink
|
||||||
int n , s, t;
|
int n , s, t;
|
||||||
|
|
||||||
//'visited' and 'visitedToken' are keep track of the
|
//'visited' and 'visitedToken' are keep track of the
|
||||||
//visited nodes.
|
//visited nodes.
|
||||||
protected:
|
protected:
|
||||||
int visitedToken = 1;
|
int visitedToken = 1;
|
||||||
int* visited;
|
int* visited;
|
||||||
|
|
||||||
|
|
||||||
bool solved;
|
bool solved;
|
||||||
long maxFlow;
|
long maxFlow;
|
||||||
|
|
||||||
//Representing the graph.
|
//Representing the graph.
|
||||||
vector<vector<Edge*>> graph;
|
vector<vector<Edge*>> graph;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
NetworkFlowSolverBase(int n, int s,int t){
|
NetworkFlowSolverBase(int n, int s,int t){
|
||||||
this->n = n;
|
this->n = n;
|
||||||
this->s = s;
|
this->s = s;
|
||||||
this->t = t;
|
this->t = t;
|
||||||
graph.resize(n);
|
graph.resize(n);
|
||||||
visited = new int[n];
|
visited = new int[n];
|
||||||
}
|
}
|
||||||
|
|
||||||
void addEdge(int from ,int to ,long capacity){
|
void addEdge(int from ,int to ,long capacity){
|
||||||
assert(capacity > 0);
|
assert(capacity > 0);
|
||||||
|
|
||||||
Edge* e1 = new Edge(from, to , capacity);
|
Edge* e1 = new Edge(from, to , capacity);
|
||||||
Edge* e2 = new Edge(to, from, 0);
|
Edge* e2 = new Edge(to, from, 0);
|
||||||
e1->residual = e2;
|
e1->residual = e2;
|
||||||
e2->residual = e1;
|
e2->residual = e1;
|
||||||
graph[from].push_back(e1);
|
graph[from].push_back(e1);
|
||||||
graph[to].push_back(e2);
|
graph[to].push_back(e2);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<vector<Edge*>> getGraph(){
|
vector<vector<Edge*>> getGraph(){
|
||||||
execute();
|
execute();
|
||||||
return graph;
|
return graph;
|
||||||
}
|
}
|
||||||
|
|
||||||
long getMaxFlow(){
|
long getMaxFlow(){
|
||||||
execute();
|
execute();
|
||||||
return maxFlow;
|
return maxFlow;
|
||||||
}
|
}
|
||||||
|
|
||||||
private :
|
private :
|
||||||
void execute(){
|
void execute(){
|
||||||
if( solved )return;
|
if( solved )return;
|
||||||
solved = true;
|
solved = true;
|
||||||
solve();
|
solve();
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void solve(){}
|
virtual void solve(){}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class FordFulkersonDfsSolver : public NetworkFlowSolverBase{
|
class FordFulkersonDfsSolver : public NetworkFlowSolverBase{
|
||||||
public :
|
public :
|
||||||
FordFulkersonDfsSolver(int n, int s, int t) : NetworkFlowSolverBase(n,s,t){
|
FordFulkersonDfsSolver(int n, int s, int t) : NetworkFlowSolverBase(n,s,t){
|
||||||
}
|
}
|
||||||
|
|
||||||
void solve(){
|
void solve(){
|
||||||
for( long f = dfs(s, INF) ; f != 0 ; f = dfs(s,INF)){
|
for( long f = dfs(s, INF) ; f != 0 ; f = dfs(s,INF)){
|
||||||
visitedToken++;
|
visitedToken++;
|
||||||
maxFlow += f;
|
maxFlow += f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private :
|
private :
|
||||||
long dfs(int node, long flow){
|
long dfs(int node, long flow){
|
||||||
if( node == t )return flow;
|
if( node == t )return flow;
|
||||||
|
|
||||||
visited[node] = visitedToken;
|
visited[node] = visitedToken;
|
||||||
|
|
||||||
vector<Edge*> edges = graph[node];
|
vector<Edge*> edges = graph[node];
|
||||||
for(Edge* edge : edges){
|
for(Edge* edge : edges){
|
||||||
if( edge->remainingCapacity() > 0 && visited[edge->to] != visitedToken){
|
if( edge->remainingCapacity() > 0 && visited[edge->to] != visitedToken){
|
||||||
long bottleNeck = dfs(edge->to , min(flow,edge->remainingCapacity()));
|
long bottleNeck = dfs(edge->to , min(flow,edge->remainingCapacity()));
|
||||||
|
|
||||||
if( bottleNeck > 0){
|
if( bottleNeck > 0){
|
||||||
edge->augment(bottleNeck);
|
edge->augment(bottleNeck);
|
||||||
return bottleNeck;
|
return bottleNeck;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
int n = 12;
|
int n = 12;
|
||||||
|
|
||||||
int s = n - 2;
|
int s = n - 2;
|
||||||
int t = n - 1;
|
int t = n - 1;
|
||||||
|
|
||||||
NetworkFlowSolverBase* solver = new FordFulkersonDfsSolver(n,s,t);
|
NetworkFlowSolverBase* solver = new FordFulkersonDfsSolver(n,s,t);
|
||||||
|
|
||||||
//Edges from Source
|
//Edges from Source
|
||||||
solver->addEdge(s, 0, 10);
|
solver->addEdge(s, 0, 10);
|
||||||
solver->addEdge(s, 1, 5);
|
solver->addEdge(s, 1, 5);
|
||||||
solver->addEdge(s, 2, 10);
|
solver->addEdge(s, 2, 10);
|
||||||
|
|
||||||
//Middle edges
|
//Middle edges
|
||||||
solver->addEdge(0, 3, 10);
|
solver->addEdge(0, 3, 10);
|
||||||
solver->addEdge(1, 2, 10);
|
solver->addEdge(1, 2, 10);
|
||||||
solver->addEdge(2, 5, 15);
|
solver->addEdge(2, 5, 15);
|
||||||
solver->addEdge(3, 1, 2);
|
solver->addEdge(3, 1, 2);
|
||||||
solver->addEdge(3, 6, 15);
|
solver->addEdge(3, 6, 15);
|
||||||
solver->addEdge(4, 1, 15);
|
solver->addEdge(4, 1, 15);
|
||||||
solver->addEdge(4, 3, 3);
|
solver->addEdge(4, 3, 3);
|
||||||
solver->addEdge(5, 4, 4);
|
solver->addEdge(5, 4, 4);
|
||||||
solver->addEdge(5, 8, 10);
|
solver->addEdge(5, 8, 10);
|
||||||
solver->addEdge(6, 7, 10);
|
solver->addEdge(6, 7, 10);
|
||||||
solver->addEdge(7, 4, 10);
|
solver->addEdge(7, 4, 10);
|
||||||
solver->addEdge(7, 5, 7);
|
solver->addEdge(7, 5, 7);
|
||||||
|
|
||||||
//Edges to sink
|
//Edges to sink
|
||||||
solver->addEdge(6, t, 15);
|
solver->addEdge(6, t, 15);
|
||||||
solver->addEdge(8, t, 10);
|
solver->addEdge(8, t, 10);
|
||||||
|
|
||||||
cout << "Maximum Flow is: " << solver->getMaxFlow() << '\n';
|
cout << "Maximum Flow is: " << solver->getMaxFlow() << '\n';
|
||||||
|
|
||||||
vector<vector<Edge*>> resultGraph = solver->getGraph();
|
vector<vector<Edge*>> resultGraph = solver->getGraph();
|
||||||
|
|
||||||
for(vector<Edge*> edges : resultGraph ){
|
for(vector<Edge*> edges : resultGraph ){
|
||||||
for( Edge* e : edges){
|
for( Edge* e : edges){
|
||||||
cout << e->toString(s,t) << '\n';
|
cout << e->toString(s,t) << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
vector<int> dist(n+1,(int)-1e17);
|
vector<int> dist(n+1,(int)-1e17);
|
||||||
|
|
||||||
dist[1]=0;
|
dist[1]=0;
|
||||||
|
|
||||||
for(int i = 1 ; i < n ; i++ ){
|
for(int i = 1 ; i < n ; i++ ){
|
||||||
for(auto& e : g ){
|
for(auto& e : g ){
|
||||||
int a,b,w;
|
int a,b,w;
|
||||||
tie(a,b,w)=e;
|
tie(a,b,w)=e;
|
||||||
dist[b]=max(dist[b],dist[a]+w);
|
dist[b]=max(dist[b],dist[a]+w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& e : g ){
|
for(auto& e : g ){
|
||||||
int a,b,w;
|
int a,b,w;
|
||||||
tie(a,b,w)=e;
|
tie(a,b,w)=e;
|
||||||
if( dist[b] < dist[a] + w ){
|
if( dist[b] < dist[a] + w ){
|
||||||
cycle[b]=true;
|
cycle[b]=true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
for(int k = 1 ; k <= n ; k++ ){
|
for(int k = 1 ; k <= n ; k++ ){
|
||||||
for(int i = 1 ; i <= n ; i++ ){
|
for(int i = 1 ; i <= n ; i++ ){
|
||||||
for(int j = 1 ; j <= n ; j++ ){
|
for(int j = 1 ; j <= n ; j++ ){
|
||||||
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
|
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,17 @@
|
|||||||
priority_queue<pii> q;
|
priority_queue<pii> q;
|
||||||
q.push({0,1});
|
q.push({0,1});
|
||||||
dist[1]=0;
|
dist[1]=0;
|
||||||
while(!q.empty()){
|
while(!q.empty()){
|
||||||
pii n=q.top();q.pop();
|
pii n=q.top();q.pop();
|
||||||
int weight=n.F,node=n.S;
|
int weight=n.F,node=n.S;
|
||||||
|
|
||||||
if( vis[node] )continue;
|
if( vis[node] )continue;
|
||||||
vis[node]=true;
|
vis[node]=true;
|
||||||
for(pii& nbr : g[node]){
|
for(pii& nbr : g[node]){
|
||||||
int n_nbr=nbr.F,n_w=nbr.S;
|
int n_nbr=nbr.F,n_w=nbr.S;
|
||||||
if( dist[node]+n_w<dist[n_nbr] ){
|
if( dist[node]+n_w<dist[n_nbr] ){
|
||||||
dist[n_nbr]=dist[node]+n_w;
|
dist[n_nbr]=dist[node]+n_w;
|
||||||
q.push({-dist[n_nbr],n_nbr});
|
q.push({-dist[n_nbr],n_nbr});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
41
Number Theory/Dp_nCr.cpp
Normal file
41
Number Theory/Dp_nCr.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<vector<int>> nCr;
|
||||||
|
void binomialCoefficient(int n,int k){
|
||||||
|
|
||||||
|
nCr.resize(n+1,vector<int>(k+1,0));
|
||||||
|
|
||||||
|
nCr[1][0] = nCr[1][1] = 1;
|
||||||
|
|
||||||
|
for(int i = 2 ; i <= n ; i++ ){
|
||||||
|
for(int j = 0 ; j <= min(i,k) ; j++ ){
|
||||||
|
|
||||||
|
if( j == 0 ){
|
||||||
|
nCr[i][j] = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( j == 1 || j == 0 ){
|
||||||
|
nCr[i][j] = i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
nCr[i][j] = nCr[i-1][j]+nCr[i-1][j-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc,char* argv[]){
|
||||||
|
|
||||||
|
binomialCoefficient(5,5);
|
||||||
|
|
||||||
|
for(int i = 0 ; i <= 5 ; i++ ){
|
||||||
|
for(int j = 0 ; j <= 5 ; j++ ){
|
||||||
|
cout << nCr[i][j] << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int binary_multiplication(int a , int b,int M=(int)1e9+7){
|
int binary_multiplication(int a , int b,int M=(int)1e9+7){
|
||||||
int res = 0;
|
int res = 0;
|
||||||
while( b ){
|
while( b ){
|
||||||
if( b&1 ) res += a , res %= M;
|
if( b&1 ) res += a , res %= M;
|
||||||
a = 2 * a ; a %= M:
|
a = 2 * a ; a %= M:
|
||||||
b >>= 1;
|
b >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,92 +1,92 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
#define ll long long int
|
#define ll long long int
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const int M = (int)1e9+7;
|
const int M = (int)1e9+7;
|
||||||
struct Mat{
|
struct Mat{
|
||||||
ll m[2][2];
|
ll m[2][2];
|
||||||
|
|
||||||
Mat(){
|
Mat(){
|
||||||
memset(m,0,sizeof(m));
|
memset(m,0,sizeof(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
void identity(){
|
void identity(){
|
||||||
for(int idx = 0 ; idx < 2 ; idx++ ){
|
for(int idx = 0 ; idx < 2 ; idx++ ){
|
||||||
m[idx][idx] = 1;
|
m[idx][idx] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Mat operator* (Mat m1,Mat m2){
|
Mat operator* (Mat m1,Mat m2){
|
||||||
Mat res;
|
Mat res;
|
||||||
|
|
||||||
for(int i = 0 ; i < 2 ; i++ ){
|
for(int i = 0 ; i < 2 ; i++ ){
|
||||||
for(int j = 0 ; j < 2 ; j++ ){
|
for(int j = 0 ; j < 2 ; j++ ){
|
||||||
for(int k = 0 ; k < 2 ; k++ ){
|
for(int k = 0 ; k < 2 ; k++ ){
|
||||||
res.m[i][j] += m1.m[i][k]*m2.m[k][j];
|
res.m[i][j] += m1.m[i][k]*m2.m[k][j];
|
||||||
res.m[i][j] %= M;
|
res.m[i][j] %= M;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat power(Mat X,ll n){
|
Mat power(Mat X,ll n){
|
||||||
Mat res;
|
Mat res;
|
||||||
res.identity();
|
res.identity();
|
||||||
|
|
||||||
ll b = n;
|
ll b = n;
|
||||||
while(b){
|
while(b){
|
||||||
if( b&1 ) res = res*X;
|
if( b&1 ) res = res*X;
|
||||||
X = X*X;
|
X = X*X;
|
||||||
b >>= 1;
|
b >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
ll fibo(ll n){
|
ll fibo(ll n){
|
||||||
|
|
||||||
if( n < 2 ) return n;
|
if( n < 2 ) return n;
|
||||||
Mat res;
|
Mat res;
|
||||||
|
|
||||||
Mat X;
|
Mat X;
|
||||||
X.m[0][1] = 1 , X.m[1][0] = 1 , X.m[1][1] = 1;
|
X.m[0][1] = 1 , X.m[1][0] = 1 , X.m[1][1] = 1;
|
||||||
|
|
||||||
res = power(X,n);
|
res = power(X,n);
|
||||||
|
|
||||||
ll fn[2][1];
|
ll fn[2][1];
|
||||||
fn[0][0]=0,fn[1][0]=1;
|
fn[0][0]=0,fn[1][0]=1;
|
||||||
|
|
||||||
ll fn1[2][1];
|
ll fn1[2][1];
|
||||||
|
|
||||||
for(int i = 0 ; i < 2 ; i++ ){
|
for(int i = 0 ; i < 2 ; i++ ){
|
||||||
for(int j = 0 ; j < 1 ; j++ ){
|
for(int j = 0 ; j < 1 ; j++ ){
|
||||||
for(int k = 0 ; k < 2 ; k++ ){
|
for(int k = 0 ; k < 2 ; k++ ){
|
||||||
fn1[i][j] = res.m[i][k]*fn[k][j];
|
fn1[i][j] = res.m[i][k]*fn[k][j];
|
||||||
fn1[i][j]%=M;
|
fn1[i][j]%=M;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fn1[0][0];
|
return fn1[0][0];
|
||||||
}
|
}
|
||||||
|
|
||||||
ll modSub(ll a,ll b){
|
ll modSub(ll a,ll b){
|
||||||
return ((a%M)-(b%M)+M)%M;
|
return ((a%M)-(b%M)+M)%M;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
int t;
|
int t;
|
||||||
cin>>t;
|
cin>>t;
|
||||||
|
|
||||||
while(t--){
|
while(t--){
|
||||||
ll n,m;
|
ll n,m;
|
||||||
cin>>n>>m;
|
cin>>n>>m;
|
||||||
|
|
||||||
cout << modSub(fibo(m+2),fibo(n+1)) << '\n';
|
cout << modSub(fibo(m+2),fibo(n+1)) << '\n';
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
//Extended gcd -> Recursive
|
//Extended gcd -> Recursive
|
||||||
|
|
||||||
tuple<int,int,int> extended_gcd( int a, int b){
|
tuple<int,int,int> extended_gcd( int a, int b){
|
||||||
|
|
||||||
if( b == 0 ){
|
if( b == 0 ){
|
||||||
return {1,0,a};
|
return {1,0,a};
|
||||||
}
|
}
|
||||||
|
|
||||||
int x,y,g;
|
int x,y,g;
|
||||||
tie(x,y,g) = extended_gcd( b , a%b );
|
tie(x,y,g) = extended_gcd( b , a%b );
|
||||||
return {y , x - (a/b)*y , g};
|
return {y , x - (a/b)*y , g};
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
int invFac[MAX_N];
|
int invFac[MAX_N];
|
||||||
void inverse_factorial(){
|
void inverse_factorial(){
|
||||||
|
|
||||||
invFac[0] = invFac[1] = 1;
|
invFac[0] = invFac[1] = 1;
|
||||||
|
|
||||||
for(int i = 2 ; i <= MAX_N ; i++ ){
|
for(int i = 2 ; i <= MAX_N ; i++ ){
|
||||||
invFac[i] = (inverse(i)*invFac[i-1])%M;
|
invFac[i] = (inverse(i)*invFac[i-1])%M;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,24 @@
|
|||||||
vector<int> invs( vi& a , int m ){
|
vector<int> invs( vi& a , int m ){
|
||||||
int n = (int)a.size();
|
int n = (int)a.size();
|
||||||
|
|
||||||
if( n == 0 ) return {};
|
if( n == 0 ) return {};
|
||||||
|
|
||||||
vector<int> b(n);
|
vector<int> b(n);
|
||||||
|
|
||||||
int v = 1;
|
int v = 1;
|
||||||
for(int i = 0 ; i < n ; i++ ){
|
for(int i = 0 ; i < n ; i++ ){
|
||||||
b[i] = v;
|
b[i] = v;
|
||||||
v = ((long long)v * a[i] ) % m;
|
v = ((long long)v * a[i] ) % m;
|
||||||
}
|
}
|
||||||
|
|
||||||
int x = power( v , m - 2 , m ) ;
|
int x = power( v , m - 2 , m ) ;
|
||||||
|
|
||||||
x = (x % m + m ) % m;
|
x = (x % m + m ) % m;
|
||||||
|
|
||||||
for(int i = n - 1 ; i >= 0 ; i-- ){
|
for(int i = n - 1 ; i >= 0 ; i-- ){
|
||||||
b[i] = x * b[i] %m;
|
b[i] = x * b[i] %m;
|
||||||
x = x * a[i] % m;
|
x = x * a[i] % m;
|
||||||
}
|
}
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
T modAdd(T a,T b,T M=(T)1e9+7){
|
T modAdd(T a,T b,T M=(T)1e9+7){
|
||||||
return ((M+a%M)%M+(M+b%M)%M)%M;
|
return ((M+a%M)%M+(M+b%M)%M)%M;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
T modMul(T a,T b,T M=(int)1e9+7){
|
T modMul(T a,T b,T M=(int)1e9+7){
|
||||||
return ((a%M)*(b%M))%M;
|
return ((a%M)*(b%M))%M;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
T modSub(T a,T b,T M=(T)1e9+7){
|
T modSub(T a,T b,T M=(T)1e9+7){
|
||||||
return (M+((M+a%M)%M-(M+b%M)%M))%M;
|
return (M+((M+a%M)%M-(M+b%M)%M))%M;
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
#define MAX_N 5000001
|
#define MAX_N 5000001
|
||||||
bool is_prime[MAX_N];
|
bool is_prime[MAX_N];
|
||||||
|
|
||||||
bool is_sieve_eval = false;
|
bool is_sieve_eval = false;
|
||||||
|
|
||||||
void sieve_erathosis(){
|
void sieve_erathosis(){
|
||||||
if( is_sieve_eval ) return;
|
if( is_sieve_eval ) return;
|
||||||
is_sieve_eval = true;
|
is_sieve_eval = true;
|
||||||
memset(is_prime,0,sizeof(is_prime));
|
memset(is_prime,0,sizeof(is_prime));
|
||||||
is_prime[0] = true , is_prime[1] = true;
|
is_prime[0] = true , is_prime[1] = true;
|
||||||
for(int num = 2 ; num*num < MAX_N ; num++ ){
|
for(int num = 2 ; num*num < MAX_N ; num++ ){
|
||||||
if( !is_prime[num] ){
|
if( !is_prime[num] ){
|
||||||
for(int val = num*num ; val < MAX_N ; val += num ){
|
for(int val = num*num ; val < MAX_N ; val += num ){
|
||||||
is_prime[val] = true;
|
is_prime[val] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
int operator%(string& a , int b){
|
int operator%(string& a , int b){
|
||||||
|
|
||||||
int sz = (int)a.size();
|
int sz = (int)a.size();
|
||||||
int val = 0;
|
int val = 0;
|
||||||
for(int idx = 0 ; idx < sz ; idx++ ){
|
for(int idx = 0 ; idx < sz ; idx++ ){
|
||||||
val = 10*val + (a[idx]-'0');
|
val = 10*val + (a[idx]-'0');
|
||||||
val %= b;
|
val %= b;
|
||||||
}
|
}
|
||||||
|
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
//Advanced Binary Seach.
|
//Advanced Binary Seach.
|
||||||
int max_iter;
|
int max_iter;
|
||||||
double low , high , ans;
|
double low , high , ans;
|
||||||
double err ;
|
double err ;
|
||||||
for(int i = 1 ; i <= max_iter ; i++ ){
|
for(int i = 1 ; i <= max_iter ; i++ ){
|
||||||
double mid = low + (high - low)/2;
|
double mid = low + (high - low)/2;
|
||||||
|
|
||||||
if( check(mid) ){
|
if( check(mid) ){
|
||||||
ans = mid;
|
ans = mid;
|
||||||
low = mid + err;
|
low = mid + err;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
high = mid - err;
|
high = mid - err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//Binary Search Jump.
|
//Binary Search Jump.
|
||||||
int ans = n;
|
int ans = n;
|
||||||
for(int b = n/2 ; b >= 1 ; b/=2 ){
|
for(int b = n/2 ; b >= 1 ; b/=2 ){
|
||||||
while(check(ans - b))ans -= b;
|
while(check(ans - b))ans -= b;
|
||||||
}
|
}
|
||||||
cout << ans << endl;
|
cout << ans << endl;
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
//Binary Search Normal
|
//Binary Search Normal
|
||||||
int low = 0 , high = n , ans;
|
int low = 0 , high = n , ans;
|
||||||
|
|
||||||
while( low <= high ){
|
while( low <= high ){
|
||||||
int mid = low + ( high - low)/2;
|
int mid = low + ( high - low)/2;
|
||||||
|
|
||||||
if( check(mid) ){
|
if( check(mid) ){
|
||||||
ans = mid;
|
ans = mid;
|
||||||
low = mid + 1;
|
low = mid + 1;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
high = mid - 1;
|
high = mid - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
double ternary_search(double l, double r) {
|
double ternary_search(double l, double r) {
|
||||||
double eps = 1e-9; //set the error limit here
|
double eps = 1e-9; //set the error limit here
|
||||||
while (r - l > eps) {
|
while (r - l > eps) {
|
||||||
double m1 = l + (r - l) / 3;
|
double m1 = l + (r - l) / 3;
|
||||||
double m2 = r - (r - l) / 3;
|
double m2 = r - (r - l) / 3;
|
||||||
double f1 = f(m1); //evaluates the function at m1
|
double f1 = f(m1); //evaluates the function at m1
|
||||||
double f2 = f(m2); //evaluates the function at m2
|
double f2 = f(m2); //evaluates the function at m2
|
||||||
if (f1 < f2)
|
if (f1 < f2)
|
||||||
l = m1;
|
l = m1;
|
||||||
else
|
else
|
||||||
r = m2;
|
r = m2;
|
||||||
}
|
}
|
||||||
return f(l); //return the maximum of f(x) in [l, r]
|
return f(l); //return the maximum of f(x) in [l, r]
|
||||||
}
|
}
|
||||||
@@ -1,137 +1,137 @@
|
|||||||
class String_hash{
|
class String_hash{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
vector<int> p = {31,33};
|
vector<int> p = {31,33};
|
||||||
|
|
||||||
vector<int> prime = { 1000000007, 1000000009 };
|
vector<int> prime = { 1000000007, 1000000009 };
|
||||||
|
|
||||||
int sz;
|
int sz;
|
||||||
|
|
||||||
//For Storing the hash value of the prefix string.
|
//For Storing the hash value of the prefix string.
|
||||||
vector<vector<int>> hash;
|
vector<vector<int>> hash;
|
||||||
//For storing the power of p(constant) for polynomial function.
|
//For storing the power of p(constant) for polynomial function.
|
||||||
vector<vector<int>> power_p;
|
vector<vector<int>> power_p;
|
||||||
//For storing the inverse of the power of p
|
//For storing the inverse of the power of p
|
||||||
vector<vector<int>> inv_power_p;
|
vector<vector<int>> inv_power_p;
|
||||||
|
|
||||||
String_hash(string a, int num){
|
String_hash(string a, int num){
|
||||||
sz = num;
|
sz = num;
|
||||||
|
|
||||||
hash.resize(sz);
|
hash.resize(sz);
|
||||||
power_p.resize(sz);
|
power_p.resize(sz);
|
||||||
inv_power_p.resize(sz);
|
inv_power_p.resize(sz);
|
||||||
string s = a;
|
string s = a;
|
||||||
int n = (int)s.size();
|
int n = (int)s.size();
|
||||||
|
|
||||||
//Pre-Calculating power.
|
//Pre-Calculating power.
|
||||||
for(int i = 0 ; i < sz ; i++ ){
|
for(int i = 0 ; i < sz ; i++ ){
|
||||||
|
|
||||||
power_p[i].resize(n);
|
power_p[i].resize(n);
|
||||||
power_p[i][0] = 1;
|
power_p[i][0] = 1;
|
||||||
|
|
||||||
for(int j = 1 ; j < n ; j++ ){
|
for(int j = 1 ; j < n ; j++ ){
|
||||||
power_p[i][j] = ( power_p[i][j-1] * p[i] ) % prime[i];
|
power_p[i][j] = ( power_p[i][j-1] * p[i] ) % prime[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pre-Calculating inverse power.
|
//Pre-Calculating inverse power.
|
||||||
for(int i = 0 ; i < sz ; i++ ){
|
for(int i = 0 ; i < sz ; i++ ){
|
||||||
|
|
||||||
inv_power_p[i] = invs(power_p[i] , prime[i] );
|
inv_power_p[i] = invs(power_p[i] , prime[i] );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Calculating prefix-hash of the given string.
|
//Calculating prefix-hash of the given string.
|
||||||
for(int i = 0 ; i < sz ; i++ ){
|
for(int i = 0 ; i < sz ; i++ ){
|
||||||
|
|
||||||
hash[i].resize(n);
|
hash[i].resize(n);
|
||||||
|
|
||||||
for(int j = 0 ; j < n ; j++ ){
|
for(int j = 0 ; j < n ; j++ ){
|
||||||
hash[i][j] = ( ( s[j] - 'a' + 1LL ) * power_p[i][j] ) % prime[i];
|
hash[i][j] = ( ( s[j] - 'a' + 1LL ) * power_p[i][j] ) % prime[i];
|
||||||
hash[i][j] = ( hash[i][j] + ( j-1 >= 0 ? hash[i][j-1] : 0LL ) ) % prime[i];
|
hash[i][j] = ( hash[i][j] + ( j-1 >= 0 ? hash[i][j-1] : 0LL ) ) % prime[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<int> invs( vi& a , int m ){
|
vector<int> invs( vi& a , int m ){
|
||||||
int n = (int)a.size();
|
int n = (int)a.size();
|
||||||
|
|
||||||
if( n == 0 ) return {};
|
if( n == 0 ) return {};
|
||||||
|
|
||||||
vector<int> b(n);
|
vector<int> b(n);
|
||||||
|
|
||||||
int v = 1;
|
int v = 1;
|
||||||
for(int i = 0 ; i < n ; i++ ){
|
for(int i = 0 ; i < n ; i++ ){
|
||||||
b[i] = v;
|
b[i] = v;
|
||||||
v = ((long long)v * a[i] ) % m;
|
v = ((long long)v * a[i] ) % m;
|
||||||
}
|
}
|
||||||
|
|
||||||
int x = power( v , m - 2 , m ) ;
|
int x = power( v , m - 2 , m ) ;
|
||||||
|
|
||||||
x = (x % m + m ) % m;
|
x = (x % m + m ) % m;
|
||||||
|
|
||||||
for(int i = n - 1 ; i >= 0 ; i-- ){
|
for(int i = n - 1 ; i >= 0 ; i-- ){
|
||||||
b[i] = x * b[i] %m;
|
b[i] = x * b[i] %m;
|
||||||
x = x * a[i] % m;
|
x = x * a[i] % m;
|
||||||
}
|
}
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_char_back(char c ){
|
void add_char_back(char c ){
|
||||||
|
|
||||||
|
|
||||||
for(int i = 0 ; i < sz ; i++ ){
|
for(int i = 0 ; i < sz ; i++ ){
|
||||||
|
|
||||||
//power of p for new character adding at the back.
|
//power of p for new character adding at the back.
|
||||||
power_p[i].pb( power_p[i].back() * p[i] );
|
power_p[i].pb( power_p[i].back() * p[i] );
|
||||||
hash[i].pb( (hash[i].back()) + ( c - 'a' + 1LL ) * (power_p[i].back()) % prime[i]);
|
hash[i].pb( (hash[i].back()) + ( c - 'a' + 1LL ) * (power_p[i].back()) % prime[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void add_char_front(char c ){
|
void add_char_front(char c ){
|
||||||
|
|
||||||
for(int i = 0 ; i < sz ; i++ ){
|
for(int i = 0 ; i < sz ; i++ ){
|
||||||
|
|
||||||
//power of p for new character adding at the back.
|
//power of p for new character adding at the back.
|
||||||
power_p[i].pb( power_p[i].back() * p[i] );
|
power_p[i].pb( power_p[i].back() * p[i] );
|
||||||
hash[i].pb( (hash[i].back()) * (power_p[i].back()) + ( c - 'a' + 1LL ) % prime[i]);
|
hash[i].pb( (hash[i].back()) * (power_p[i].back()) + ( c - 'a' + 1LL ) % prime[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<int> subStringHash(int l , int r ){ //log 1e9+7
|
vector<int> subStringHash(int l , int r ){ //log 1e9+7
|
||||||
|
|
||||||
vector<int> new_hash(sz);
|
vector<int> new_hash(sz);
|
||||||
for(int i = 0 ; i < sz ; i++ ){
|
for(int i = 0 ; i < sz ; i++ ){
|
||||||
|
|
||||||
int val1 = ( l-1 >= 0 ? hash[i][l-1] : 0LL );
|
int val1 = ( l-1 >= 0 ? hash[i][l-1] : 0LL );
|
||||||
int val2 = hash[i][r];
|
int val2 = hash[i][r];
|
||||||
|
|
||||||
new_hash[i] = modMul( modSub( val2 , val1 , prime[i]) , inv_power_p[i][l] , prime[i] ) ;
|
new_hash[i] = modMul( modSub( val2 , val1 , prime[i]) , inv_power_p[i][l] , prime[i] ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_hash;
|
return new_hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool compareSubString( int l1 , int r1 , int l2 , int r2 ){
|
bool compareSubString( int l1 , int r1 , int l2 , int r2 ){
|
||||||
|
|
||||||
if( l1 > l2 ){
|
if( l1 > l2 ){
|
||||||
swap(l1,l2);
|
swap(l1,l2);
|
||||||
swap(r1,r2);
|
swap(r1,r2);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0 ; i < sz ; i++ ){
|
for(int i = 0 ; i < sz ; i++ ){
|
||||||
|
|
||||||
int val1 = modSub( hash[i][r1] , ( l1 - 1 >= 0 ? hash[i][l1-1] : 0LL ), prime[i] );
|
int val1 = modSub( hash[i][r1] , ( l1 - 1 >= 0 ? hash[i][l1-1] : 0LL ), prime[i] );
|
||||||
int val2 = modSub( hash[i][r2] , ( l2 - 1 >= 0 ? hash[i][l2-1] : 0LL ), prime[i] );
|
int val2 = modSub( hash[i][r2] , ( l2 - 1 >= 0 ? hash[i][l2-1] : 0LL ), prime[i] );
|
||||||
|
|
||||||
if( modMul( val1 ,power_p[i][l2-l1] , prime[i] ) != val2 ){
|
if( modMul( val1 ,power_p[i][l2-l1] , prime[i] ) != val2 ){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
350
cpadv.cpp
350
cpadv.cpp
@@ -1,175 +1,175 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
//#include <ext/pb_ds/assoc_container.hpp>
|
//#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
|
||||||
//using namespace __gnu_pbds;
|
//using namespace __gnu_pbds;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// template<typename T>
|
// template<typename T>
|
||||||
// using indexed_multiset = tree<T , null_type , less_equal<T> , rb_tree_tag ,
|
// using indexed_multiset = tree<T , null_type , less_equal<T> , rb_tree_tag ,
|
||||||
// tree_order_statistics_node_update>;
|
// tree_order_statistics_node_update>;
|
||||||
// template<typename T>
|
// template<typename T>
|
||||||
// using indexed_set=tree<T,null_type,less<T>,rb_tree_tag,
|
// using indexed_set=tree<T,null_type,less<T>,rb_tree_tag,
|
||||||
// tree_order_statistics_node_update>;
|
// tree_order_statistics_node_update>;
|
||||||
|
|
||||||
#define ld long double
|
#define ld long double
|
||||||
#define ll long long int
|
#define ll long long int
|
||||||
#define F first
|
#define F first
|
||||||
#define S second
|
#define S second
|
||||||
#define pb push_back
|
#define pb push_back
|
||||||
#define si set <int>
|
#define si set <int>
|
||||||
#define sll set <ll>
|
#define sll set <ll>
|
||||||
#define vi vector <int>
|
#define vi vector <int>
|
||||||
#define vll vector <ll>
|
#define vll vector <ll>
|
||||||
#define pii pair <int, int>
|
#define pii pair <int, int>
|
||||||
#define pll pair <ll,ll>
|
#define pll pair <ll,ll>
|
||||||
#define vpi vector <pii>
|
#define vpi vector <pii>
|
||||||
#define vpl vector <pll>
|
#define vpl vector <pll>
|
||||||
#define mii map <int, int>
|
#define mii map <int, int>
|
||||||
#define mll map <ll,ll>
|
#define mll map <ll,ll>
|
||||||
#define sz(x) ((int) x.size())
|
#define sz(x) ((int) x.size())
|
||||||
#define all(p) p.begin(), p.end()
|
#define all(p) p.begin(), p.end()
|
||||||
#define que_max priority_queue <int>
|
#define que_max priority_queue <int>
|
||||||
#define que_max_ll priority_queue <ll>
|
#define que_max_ll priority_queue <ll>
|
||||||
#define que_min priority_queue <int, vi, greater<int>>
|
#define que_min priority_queue <int, vi, greater<int>>
|
||||||
#define que_min_ll priority_queue <ll , vll, greater<ll>>
|
#define que_min_ll priority_queue <ll , vll, greater<ll>>
|
||||||
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
|
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
|
||||||
#define print(a) for(auto x : a) cout << x << " "; cout << endl
|
#define print(a) for(auto x : a) cout << x << " "; cout << endl
|
||||||
#define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
|
#define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
|
||||||
#define print2(i,a,x,y) for(auto i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
|
#define print2(i,a,x,y) for(auto i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
|
||||||
#define scan(s,e,a) for(auto i = s ; i <= e ; i++ ) cin>>a[i]
|
#define scan(s,e,a) for(auto i = s ; i <= e ; i++ ) cin>>a[i]
|
||||||
#define loop(i,s,e,weight) for(auto i = s ; (weight>=0? i <= e : i >= e ) ; i+=weight )
|
#define loop(i,s,e,weight) for(auto i = s ; (weight>=0? i <= e : i >= e ) ; i+=weight )
|
||||||
#define read(n) ll n; cin>>n
|
#define read(n) ll n; cin>>n
|
||||||
#define endl '\n'
|
#define endl '\n'
|
||||||
#define ALGO_START clock_t z = clock()
|
#define ALGO_START clock_t z = clock()
|
||||||
#define ALGO_END cout << "RUN TIME : " << fixed << setprecision(6) << ((double)(clock() - z))/CLOCKS_PER_SEC
|
#define ALGO_END cout << "RUN TIME : " << fixed << setprecision(6) << ((double)(clock() - z))/CLOCKS_PER_SEC
|
||||||
|
|
||||||
//endl -> cout << '\n' << flush;//hence,slower.
|
//endl -> cout << '\n' << flush;//hence,slower.
|
||||||
//for interactive problem.
|
//for interactive problem.
|
||||||
// cout << flush; //for cin cout
|
// cout << flush; //for cin cout
|
||||||
// fflush(stdout); //for scanf printf.
|
// fflush(stdout); //for scanf printf.
|
||||||
|
|
||||||
//In Mathematics section of cp course.
|
//In Mathematics section of cp course.
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T modMul(T a,T b,T M=(int)1e9+7){
|
T modMul(T a,T b,T M=(int)1e9+7){
|
||||||
return ((a%M)*(b%M))%M;
|
return ((a%M)*(b%M))%M;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T power(T a, T b, T M)
|
inline T power(T a, T b, T M)
|
||||||
{
|
{
|
||||||
T x = 1;
|
T x = 1;
|
||||||
while (b)
|
while (b)
|
||||||
{
|
{
|
||||||
if (b & 1) x = modMul(x,a,M);
|
if (b & 1) x = modMul(x,a,M);
|
||||||
a = modMul(a,a,M);
|
a = modMul(a,a,M);
|
||||||
b >>= 1;
|
b >>= 1;
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline T pwr(T a, T b)
|
inline T pwr(T a, T b)
|
||||||
{
|
{
|
||||||
T x = 1;
|
T x = 1;
|
||||||
while (b)
|
while (b)
|
||||||
{
|
{
|
||||||
if (b & 1) x = x*a;
|
if (b & 1) x = x*a;
|
||||||
a = a*a;
|
a = a*a;
|
||||||
b >>= 1;
|
b >>= 1;
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Square root using advanced binary search.
|
//Square root using advanced binary search.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
ld sq_root(T x){
|
ld sq_root(T x){
|
||||||
|
|
||||||
ld err = 1e-6;
|
ld err = 1e-6;
|
||||||
|
|
||||||
ld ans;
|
ld ans;
|
||||||
ld low = 1.0 , high = x;
|
ld low = 1.0 , high = x;
|
||||||
for(int i = 1 ; i <= 40 ; i++ ){
|
for(int i = 1 ; i <= 40 ; i++ ){
|
||||||
ld mid = low + (high - low)/2.0;
|
ld mid = low + (high - low)/2.0;
|
||||||
|
|
||||||
if( mid*mid <= x ){
|
if( mid*mid <= x ){
|
||||||
ans = mid;
|
ans = mid;
|
||||||
low = mid + err;
|
low = mid + err;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
high = mid - err;
|
high = mid - err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename Arg1>
|
template <typename Arg1>
|
||||||
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
|
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
|
||||||
template <typename Arg1, typename... Args>
|
template <typename Arg1, typename... Args>
|
||||||
void __f (const char* names, Arg1&& arg1, Args&&... args)
|
void __f (const char* names, Arg1&& arg1, Args&&... args)
|
||||||
{
|
{
|
||||||
const char* comma = strchr (names + 1, ',');
|
const char* comma = strchr (names + 1, ',');
|
||||||
cout.write (names, comma - names) << " : " << arg1 << " "; __f (comma + 1, args...);
|
cout.write (names, comma - names) << " : " << arg1 << " "; __f (comma + 1, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*******************************************************************Debugging tools *************************************************/
|
/*******************************************************************Debugging tools *************************************************/
|
||||||
#define dbg(x...) cerr << #x << " "; _print(x); cerr << endl;
|
#define dbg(x...) cerr << #x << " "; _print(x); cerr << endl;
|
||||||
#define debug(x,y...) cerr << #x << " -> "; _print(y); cerr << endl;
|
#define debug(x,y...) cerr << #x << " -> "; _print(y); cerr << endl;
|
||||||
#define crndl cerr << "\n";
|
#define crndl cerr << "\n";
|
||||||
|
|
||||||
template <typename T> void _print (T t) { cerr << t; }
|
template <typename T> void _print (T t) { cerr << t; }
|
||||||
void _print() {return;}
|
void _print() {return;}
|
||||||
|
|
||||||
template <class T> void _print(T a[], int n) { cerr << "[ "; for(int i = 0; i < n; i++) { _print(a[i]); cerr << " "; } cerr << "]"; }
|
template <class T> void _print(T a[], int n) { cerr << "[ "; for(int i = 0; i < n; i++) { _print(a[i]); cerr << " "; } cerr << "]"; }
|
||||||
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
|
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
|
||||||
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
|
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
|
||||||
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
|
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
|
||||||
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
|
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
|
||||||
template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto &i : v) {_print(i); cerr << " ";} cerr << "]";}
|
template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto &i : v) {_print(i); cerr << " ";} cerr << "]";}
|
||||||
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto &i : v) {_print(i); cerr << " ";} cerr << "]";}
|
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto &i : v) {_print(i); cerr << " ";} cerr << "]";}
|
||||||
template <class T> void _print(deque <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
|
template <class T> void _print(deque <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
|
||||||
template <class T> void _print(queue <T> q) {cerr << "[ "; while (!q.empty()) {_print(q.front()); cerr << " "; q.pop();} cerr << "]";}
|
template <class T> void _print(queue <T> q) {cerr << "[ "; while (!q.empty()) {_print(q.front()); cerr << " "; q.pop();} cerr << "]";}
|
||||||
template <class T> void _print(stack <T> s) {cerr << "[ "; stack<T> t; while (!s.empty()) {t.push(s.top()); s.pop();} while (!t.empty()) {_print(t.top()); cerr << " "; t.pop();} cerr << "]";}
|
template <class T> void _print(stack <T> s) {cerr << "[ "; stack<T> t; while (!s.empty()) {t.push(s.top()); s.pop();} while (!t.empty()) {_print(t.top()); cerr << " "; t.pop();} cerr << "]";}
|
||||||
template <class T> void _print(priority_queue <T> pq) {cerr << "[ "; while (!pq.empty()) {_print(pq.top()); cerr << " "; pq.pop();} cerr << "]";}
|
template <class T> void _print(priority_queue <T> pq) {cerr << "[ "; while (!pq.empty()) {_print(pq.top()); cerr << " "; pq.pop();} cerr << "]";}
|
||||||
template <class T> void _print(priority_queue<T, vector<T>, greater<T>> pq) {cerr << "[ "; while (!pq.empty()) {_print(pq.top()); cerr << " "; pq.pop();} cerr << "]";}
|
template <class T> void _print(priority_queue<T, vector<T>, greater<T>> pq) {cerr << "[ "; while (!pq.empty()) {_print(pq.top()); cerr << " "; pq.pop();} cerr << "]";}
|
||||||
|
|
||||||
template <class T, class... V> void _print(T t, V... v) {_print(t); if(sizeof...(v)) {cerr<<", "; _print(v...);}}
|
template <class T, class... V> void _print(T t, V... v) {_print(t); if(sizeof...(v)) {cerr<<", "; _print(v...);}}
|
||||||
// template <class T> void _print(oset<T> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
|
// template <class T> void _print(oset<T> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
|
||||||
|
|
||||||
/************************************************************************************************************************************/
|
/************************************************************************************************************************************/
|
||||||
|
|
||||||
const int N = 200005;
|
const int N = 200005;
|
||||||
/**********************Solve Here*********************/
|
/**********************Solve Here*********************/
|
||||||
|
|
||||||
void solve(){
|
void solve(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**********************Solve Here*********************/
|
/**********************Solve Here*********************/
|
||||||
int32_t main()
|
int32_t main()
|
||||||
{
|
{
|
||||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||||
|
|
||||||
// clock_t z = clock();
|
// clock_t z = clock();
|
||||||
|
|
||||||
/***********************Test cases********************/
|
/***********************Test cases********************/
|
||||||
|
|
||||||
|
|
||||||
int t = 1;
|
int t = 1;
|
||||||
// cin >> t;
|
// cin >> t;
|
||||||
// int i=1;
|
// int i=1;
|
||||||
while (t--){
|
while (t--){
|
||||||
// cout << "Case#" << i << endl;
|
// cout << "Case#" << i << endl;
|
||||||
solve();
|
solve();
|
||||||
// i++;
|
// i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/***********************Test cases********************/
|
/***********************Test cases********************/
|
||||||
// cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
|
// cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
|
||||||
//cerr= c error.
|
//cerr= c error.
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user