I'm suffering from one problem on codechef.com.
When I submit my code with all variables (which are only 3) in global declaration, I get Accepted, but when I move all 3 variables inside main function, I get wrong answer!! All 3 variables are long long type and IO used is scanf & printf.
long long P, Q, a;
scanf("%d %d",&P,&Q);
printf("%lld",a);
I'v tried same code translated in pascal, then I get Time limit exceed.
Also one thing, when I use my custom fast IO functions, I get Time limit exceed.
In all cases my code works fine and produce correct output in my IDE.
When I use global variables and don't write "return 0" statement though it gets accepted. But if I declare variables inside main function and don't write "return 0" statement, it stuck with non zero return code runtime error.
Can anybody tell why I'm not able to get my code accepted in case of local variable declaration or why I'm getting time limit exceed when used exact algo in Pascal?? Is there something wrong with input or something??
Here is my C code which gets accepted by codechef:
#include<stdio.h>
long long P,Q,a;
int main()
{
scanf("%d %d",&P,&Q);
//read(Q);
while (P>0 || Q>0 )
{
a = ( Q * ( 3*P - 2*(Q*Q-1) ) ) / 3;
printf("%lld\n",a);
scanf("%d %d",&P,&Q);
}
return 0;
}
And this is pascal code which gets Time limit exceed:
program ex2dimarray;
var
P,Q,a: Cardinal;
begin
readln(P,Q);
//read(Q);
while (P>0) or (Q>0) do
begin
a := ( Q * ( 3*P - 2*(Q*Q-1) ) ) DIV 3;
writeln(a);
readln(P,Q);
//read(Q);
end;
end.
Program termination condition is both P and Q is 0.
<inttypes.h>andscanf("%"SCNd64" %"SCNd64, &P, &Q);. - Brian Cain