1853B - Fibonaccharsis - CodeForces Solution


binary search brute force math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
ll countSol(ll coeff[], ll n, ll rhs)
{
    // Create and initialize a table
    // to store results of subproblems
    ll dp[rhs + 1];
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;
 
    // Fill table in bottom up manner
    for (ll i = 0; i < n; i++)
    for (ll j = coeff[i]; j <= rhs; j++)
        dp[j] += dp[j - coeff[i]];
 
    return dp[rhs];
}
ll solFinder(ll n,ll x,ll y){
	ll ans=0;
	for(ll i=0;i<=200015;i++){
		if(n-x*i>=0){
			ll r1=n-x*i;
			ll r2=(n-x*i)%y;
			ll r3=(n-x*i)/y;
			if(r2==0 and i<=r3){
				ans++;
			}
		}
		else break;
	}
	return ans;
}
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--){
		ll n,k;cin>>n>>k;
		ll z=200000;
		bool flag=true;
		if(k>z){
			cout<<0<<endl;
			continue;
		}
		vector<ll> x,y;
		x.pb(1);
		x.pb(0);
		x.pb(1);
		y.pb(0);
		y.pb(1);
		y.pb(1);
		for(int i=3;i<k;i++){
			x.pb(y[i-1]);
			y.pb(y[i-1]+x[i-1]);
			//cout<<x[i]<<" "<<y[i]<<endl;
			if(x[i]>z or y[i]>z){
				cout<<0<<endl;
				flag=false;
		    	break;
			}
		}
		
		if(flag){
			ll x1=x[k-1];
			ll y1=y[k-1];
			cout<<solFinder(n,x1,y1)<<endl;
		}
		
	}
}


Comments

Submit
0 Comments
More Questions

368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings