Problem link
This is a straightforward problem of of java BigInteger . Learn it frome here .
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scn = new Scanner(System.in); BigInteger tot,temp; int n,a; while(scn.hasNextInt()){ n=scn.nextInt(); a=scn.nextInt(); tot=new BigInteger("0"); temp=BigInteger.valueOf(a); for(int i=1;i<=n;i++){ tot=tot.add(BigInteger.valueOf(i).multiply(temp.pow(i))); } System.out.println(tot); } } } |