B - Sum of Geometric Series
Source: atcoder
Difficulty: 200

Problem Statement
You are given two positive integers
N
and
M
.
Let
X = \displaystyle\sum_{i = 0}^{M} N^i
. If
X \leq 10^9
, print the value of
X
. If
X > 10^9
, print
inf
.
Constraints
1 \leq N \leq 10^9
1 \leq M \leq 100
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
M
Output
Print the value of
X
or
inf
as specified by the problem statement.
Sample Input 1
7 3
Sample Output 1
400
X = 1 + 7 + 49 + 343 = 400
. Since
400 \leq 10^9
, print
400
.
Sample Input 2
1000000 2
Sample Output 2
inf
X = 1000001000001 > 10^9
, so print
inf
.
Sample Input 3
999999999 1
Sample Output 3
1000000000
Sample Input 4
998244353 99
Sample Output 4
inf

C++ solution:
#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ll long long
#define N 200005

ll n, m;
ll a[N], b[N];

void solve() {
    cin >> n >> m;
    ll ans = 1;
    for(int i = 1; i <= m; i++) {
        ll now = 1;
        for(int j = 1; j <= i; j++) {
            now *= n;
            if(now > 1e9) {
                cout << "inf";
                return;
            }
        }
        ans += now;
        if(ans > 1e9) {
            cout << "inf";
            return;
        }
    }
    cout << ans;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int tt = 1;
//    cin >> tt;
    while(tt--)
        solve();
    return 0;
}

Go solution:
package main
import "fmt"
func main(){
    var n,m int
    fmt.Scan(&n,&m)
    ans,tmp:=0,1
    for i:=0;i<=m;i++{
        ans+=tmp
        tmp*=n
        if ans>1_000_000_000{
            fmt.Println("inf")
            return
        }
    }
    fmt.Println(ans)
}

Java solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void solve(int a, int b) {
        int sum = 0;
        for (int i = 0; i <= b; i++) {
            sum += (int) Math.pow(a, i);
            if (sum > (Math.pow(10, 9)) || sum <= 0) {
                System.out.println("inf");
                return;
            }
        }
            if (sum > (Math.pow(10, 9)) || sum <= 0) {
                System.out.println("inf");
            } else {
                System.out.println(sum);
            }
        }

    public static void main(String[] args) throws IOException {
        BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer str = new StringTokenizer(br.readLine());
        int a = Integer.parseInt(str.nextToken());
        int b = Integer.parseInt(str.nextToken());
        solve(a,b);
    }
}

JavaScript solution:
function main(input) {
  const inputValue = input.trim().split(' ').map(item => parseInt(item))
  const base = inputValue[0]
  const count = inputValue[1]
  let sum = 0
  for (let i = 0; i <= count; i++) sum += Math.pow(base, i)
  
  sum > Math.pow(10, 9) ? console.log('inf') : console.log(sum)
}

//本番用
main(require("fs").readFileSync("/dev/stdin", "utf8"));

Python solution:
N,M = map(int,input().split())

num = 0

for i in range(M+1):
    num += N**i

if num <= 10**9:
    print(num)

else:
    print("inf")

Ruby solution:
# 20:47 -

n, m = gets.split.map(&:to_i)

def solve(n, m)
  sum = 0
  (0..m).each do |i|
    sum += n**i
    if sum > 1_000_000_000
      return 'inf'
    end
  end
  sum
end

puts solve(n, m)