Simple Recursive Sequence - HackerEarth SolutionSimple Recursive Sequence - HackerEarth Solution
Simple Recursive Sequence
The
Find the
Example
Consider K = 2 and T = 2,
as
then we get
then we get
Function description
Complete the Simple_Recursive_Sequence function provided in the editor. This function takes the following 2 parameters and returns an array containing
- K: An Integer representing value of K.
- T: An Integer representing value of T.
Input format
- First line: Two space-separated integers
and
Ouput format
Print
Constraints
Simple Recursive Sequence - HackerEarth Solution
Solution -
#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)#define t_times int t; cin >> t; while(t--) long long n, k;const int m = 3001;int vis[m][m];
void _solve() { cin >> k >> n; assert(k >= 0 && k <= 3000); assert(n >= 0 && n <= 1000000000); int a = 1, b = 2, next_a, next_b, cnt = 1, period = -1; vis[a][b] = 1; vector< pair< int, int>> v {{a, b}}; for(int i = 1; i <= m * m; i++) { next_a = (k * b + a * a) % m; next_b = (k * a + b) % m; a = next_a, b = next_b; v.push_back({a, b}); if(vis[a][b]) { period = cnt - vis[a][b] + 1; cnt = vis[a][b]; break; } vis[a][b] = ++cnt; } //cout << cnt << ' ' << period << '\n'; assert(period != -1); int pos = n; if(n > cnt) pos = cnt + ((n - cnt) % period); cout << v[pos].first << ' ' << v[pos].second << '\n';} int main() { fastio; //t_times _solve(); return 0;}
#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
long long n, k;
const int m = 3001;
int vis[m][m];
void _solve() {
cin >> k >> n;
assert(k >= 0 && k <= 3000);
assert(n >= 0 && n <= 1000000000);
int a = 1, b = 2, next_a, next_b, cnt = 1, period = -1;
vis[a][b] = 1;
vector< pair< int, int>> v {{a, b}};
for(int i = 1; i <= m * m; i++) {
next_a = (k * b + a * a) % m;
next_b = (k * a + b) % m;
a = next_a, b = next_b;
v.push_back({a, b});
if(vis[a][b]) {
period = cnt - vis[a][b] + 1;
cnt = vis[a][b];
break;
}
vis[a][b] = ++cnt;
}
//cout << cnt << ' ' << period << '\n';
assert(period != -1);
int pos = n;
if(n > cnt) pos = cnt + ((n - cnt) % period);
cout << v[pos].first << ' ' << v[pos].second << '\n';
}
int main() {
fastio;
//t_times
_solve();
return 0;
}
Post a Comment