We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Fibonacci Numbers Tree
Fibonacci Numbers Tree
Sort by
recency
|
6 Discussions
|
Please Login in order to post a comment
here is hackerrank fibonacci numbers tree solution
include
using namespace std ;
define ft first
define sd second
define pb push_back
define all(x) x.begin(),x.end()
define ull unsigned long long int
define ui unsigned int
define ll long long int
define vi vector
define vii vector >
define pii pair
define vl vector
define vll vector >
define pll pair
define mp make_pair
define sc(x) scanf("%d",&x)
define sc2(x,y) scanf("%d%d",&x,&y)
define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
define scll1(x) scanf("%lld",&x)
define scll2(x,y) scanf("%lld%lld",&x,&y)
define scll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
define pr1(x) printf("%d\n",x)
define pr2(x,y) printf("%d %d\n",x,y)
define pr3(x,y,z) printf("%d %d %d\n",x,y,z)
define prll1(x) printf("%lld\n",x)
define prll2(x,y) printf("%lld %lld\n",x,y)
define prll3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
define pr_vec(v) for(int i=0;i
define f_in(st) freopen(st,"r",stdin)
define f_out(st) freopen(st,"w",stdout)
define fr(i, a, b) for(i=a; i<=b; i++)
define fb(i, a, b) for(i=a; i>=b; i--)
include
include
define debug( s ) cout << s << "\n"const int maxn = 1e5 + 10; const int mod = 1e9 + 7; const int fib0 = 88564505; const int fib1 = 56182730; const int constant = 99999;
int n, m, ptr, lg, chainNo; vi adj[ maxn ]; int subsize[ maxn ], chainHead[ maxn ], chainInd[ maxn ], posInBase[ maxn ], Base[ maxn ], LCA[ maxn ][22]; int L[ maxn ], in[ maxn ], out[ maxn ], F[ maxn ], I[2][2], T[2][2], vis[ maxn ], heavy[ maxn ];
void dfs( int u, int par = -1 ) { L[1] = 1; queue Q; stack st; Q.push( 1 ); while( !Q.empty() ) { u = Q.front(); Q.pop(); st.push( u ); subsize[u] = 1; for( auto it: adj[u] ) { LCA[it][0] = u; L[it] = L[u] + 1; Q.push( it ); } } while( !st.empty() ) { u = st.top(); st.pop(); for( auto it: adj[u] ) { subsize[u] += subsize[it]; } } }
void HLD() { stack st; queue Q;
}
struct node { int S, Lazy0, Lazy1, F1, F2; node() { S = Lazy0 = Lazy1 = F1 = F2 = 0; } };
node st[ 4 * maxn ]; class SegmentTree { public:
};
SegmentTree SegTree; int query_up(int u, int v) { int uchain, vchain = chainInd[v]; int ans = 0; while(1) { uchain = chainInd[u]; if(uchain == vchain) { ans += SegTree.query(1, 1, n, posInBase[v], posInBase[u]); if( ans >= mod ) ans -= mod; break; } // do query from u chain’s head to u ans += SegTree.query(1, 1, n, posInBase[chainHead[uchain]], posInBase[u]); if( ans >= mod ) ans -= mod; u = chainHead[uchain]; // promote u to chain’s head and then to its parent // update answer u = LCA[u][0]; } return ans; }
void ConstructLCA() { lg = ceil(log2(n)); memset(LCA, -1, sizeof LCA); dfs(1, -1); int i, j; fr(i, 1, lg) { fr(j, 1, n) { if(LCA[j][i-1] != -1) { LCA[j][i] = LCA[LCA[j][i-1]][i-1]; } } } }
int getLca(int x, int y) { if(L[x] < L[y]) swap(x, y); for(int i=lg; i>=0; i--) { if( LCA[x][i] != -1 && L[LCA[x][i]] >= L[y] ) x = LCA[x][i]; } if( x == y ) return x; for(int i=lg; i>=0; i--) { if( LCA[x][i] != -1 && LCA[x][i] != LCA[y][i] ) x = LCA[x][i] , y = LCA[y][i]; } return LCA[x][0]; }
void reset() { I[0][0] = 1; I[0][1] = 0; I[1][0] = 0; I[1][1] = 1;
}
void mult(int A[2][2], int B[2][2]) { int temp[2][2]; int i, j, k; fr(i, 0, 1) { fr(j, 0, 1) { temp[i][j] = 0; fr(k, 0, 1) { temp[i][j] += 1LL * A[i][k] * B[k][j] % mod; if( temp[i][j] >= mod ) temp[i][j] -= mod; } } } fr(i, 0, 1) { fr(j, 0, 1) { A[i][j] = temp[i][j]; } } }
void fibo( ll x ) { reset(); while( x ) { if( x % 2 ) mult(I, T); x /= 2; mult(T, T); } }
int main() {
}
This can be done (at least in C) with sqrt(n) decomposition instead of HLD, but it's close and micro-optimizations count.
I'm confused about the way the example tree is built. Shouldn't the left-most leaf be the fourth node?
somehow my output isnt right
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
} class Node { Node left = null; Node right = null; Node parent = null; long value = 0; long id = 0; Node(long val) { this.value = val; this.left = null; this.right = null; }
}