D - Sum of Geometric Series Editorial
by
Binary_64
Solution for Problem B
So far, there is no English solution for this problem, so I wrote one.
It can be proven that if \(n\) and \(i\) are positive integers, then \(n^i\) is also a positive integer. Therefore, adding \(n^i\) to a number will always increase its value.
So we can directly compute the value through brute force. If at any point the value of \(X\) exceeds \(10^9\), then any further additions to \(X\) will also result in values greater than \(10^9\). In that case, we can directly output inf.
Otherwise, simply output the result as required by the problem.
#pragma GCC optimize(3,"Ofast","inline","unroll-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
cin.tie(0)->sync_with_stdio(false);
int n,m;cin>>n>>m;
__int128 x=0,pwr=1;
for(int i=0;i<=m;++i){
x+=pwr;
pwr*=n;
if(x>1e9){
cout<<"inf\n";
return 0;
}
}
cout<<(int)x<<'\n';
return 0;
}
S=input().split()
n=int(S[0]);m=int(S[1])
X=0
pwr=1
lim=1000000000
for i in range(m+1):
X+=pwr
if(X>lim):break
pwr*=n
if(X>lim):print('inf')
else:print(X)
posted:
last update:
