Постановка задачи
Учитывая A и R i, e первый член и обычное отношение серии GP. Найдите N-й член ряда.
Пример 1:
Input: A = 2, R = 2, N = 4 Output: 16 Explanation: The GP series is 2, 4, 8, 16, 32,... in which 16 is th 4th term.
Пример 2:
Input: A = 4, R = 3, N = 3 Output: 36 Explanation: The GP series is 4, 12, 36, 72,.. in which 36 is the 3rd term.
Ожидаемая сложность времени: O (LogN)
Ожидаемая сложность пространства: O (1)
Ограничения:
1 ‹= A, R, N‹ = 1000000
Код и алгоритм:
/* author : @akash */ /* c++ code */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define mod 1000000007 #define ld long double void solve() { int a; int R; cin>>a>>R; int n; cin>>n; cout<<a*pow(R,n-1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) { solve(); cout<<"\n"; } return 0; } // time complexity of this algorithm is : T(n)=O(LogN)
Спасибо.
Инженер-программист