My code getting TLE in sgu 112 problem. I am new to java programming so I am not understanding why my code is getting TLE. I also have submitted 3 code from github for this problem. They also got TLE. Now what should I do? Question link : http://codeforces.net/problemsets/acmsguru/problem/99999/112
My code :
//In the name of ALLAH
//package OverRiding;
import java.math.BigInteger;
import java.util.Scanner;
public class Example1 {
public static void main(String[]args) {
Scanner cin = new Scanner( System.in );
BigInteger bigA = cin.nextBigInteger();
BigInteger bigB = cin.nextBigInteger();
BigInteger aPowb, bPowa;
aPowb = bigA.pow ( bigB.intValue() );
bPowa = bigB.pow ( bigA.intValue() );
BigInteger ans = aPowb.subtract ( bPowa );
System.out.println(ans);
}
}
The Problem is in Scanner, I think. Try use another input class
Bro! please tell me about that input class which you have used to solve it. Name or tutorial link.
If your Logic is different then tell me the logic.
I use bufferedReader and stringtokenizer Don't know if there are some faster way.
Ac! Thanks! :)
I used C to solve this problem. With <math.h> using pow function I get wrong answers on test 1. Then I used a different method and tried without using the pow function and now I'm getting wrong answers on test 5. My code seems fine. I've checked my code over and over again but It doesn't work. Here's my code if anyone can answer me where I did wrong it would be a big help.
include<stdio.h>
int main()
{
}
Hmmm $$$100^{100}$$$ definitely cannot be represented as a 64-bit integer (
long long int
). Try implementing big integers by hand or use some implementations that are already published online.Oh, that's it then. I have understood the problem now. Thank you very much.
I'm a beginner so I don't really know much about big integers, bigger than long long int. I've searched for some info and found GMP library and some said to use array.
For this problem array isn't a good choice I guess. I don't know how to use GMP and couldn't find any useful easy to understand tutorial for that.
Can you give me a direction where to look, or how the implementations of big integers works in C?
Thank you for your time. Hope you and your family are doing okay in this pandemic.
God bless us all.
A and b are big number it can be 100^100 — 100^100 so the long long can't store this huge value you want to store it in a string and subtract it their is a good article in geeksforgeeks
https://www.geeksforgeeks.org/difference-of-two-large-numbers/
I am not java expert but I think there should be something like cin.close() at the end, or System.exit(0), or System.out.flush(). The calculation does not need much time.