Why is my virtual judge solution giving me wrong answers?

  • Thread starter Thread starter fireflies
  • Start date Start date
  • Tags Tags
    Judge Virtual
Click For Summary

Discussion Overview

The discussion revolves around a coding problem from a virtual judge platform, where participants are trying to determine why a submitted solution is producing incorrect answers. The problem involves calculating outputs based on a recursive function with specific inputs, and participants are sharing code snippets and debugging efforts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares their initial code and expresses confusion over receiving wrong answers, particularly for a critical input-output case.
  • Another participant provides a different version of the code, highlighting potential integer overflow issues and the need for careful handling of outputs.
  • Several participants request clarification on the original problem statement and emphasize the importance of using code tags for readability.
  • A later reply points out a specific coding error related to array initialization, suggesting that the original poster may have made a mistake in their code.
  • One participant mentions that they have updated their code but are still encountering wrong answers, indicating ongoing issues with their solution.
  • Another participant confirms that they were able to resolve their issues and get their code accepted, thanking others for their help.

Areas of Agreement / Disagreement

Participants generally agree on the need for clarity in code presentation and the importance of addressing potential coding errors. However, there is no consensus on the specific reasons for the wrong answers, as multiple interpretations and solutions are presented.

Contextual Notes

Some participants note that the problem may involve integer overflow, and there are indications that the recursive function may not be optimized for performance given the constraints of the problem.

Who May Find This Useful

This discussion may be useful for individuals interested in competitive programming, debugging recursive functions, or understanding common pitfalls in coding challenges related to output correctness and performance optimization.

fireflies
Messages
210
Reaction score
13
<< Mentor Note -- Posts edited to add code tags for better readability >>[/color]

Hello everyone-

The vjudge problem link is: http://www.lightoj.com/volume_showproblem.php?problem=1006

My solution is:

C:
#include <iostream>

using namespace std;

long long int arr[10001];
long long int a,b,c,d,e,f;

long long int F(int n)
{
    if(n==0)
    {
        arr[n]=a;
        return arr[n];
    }
    if(n==1)
    {
        arr[n]=b;
        return arr[n];
    }
    if(n==2)
    {
        arr[n]=c;
        return arr[n];
    }
    if(n==3)
    {
        arr[n]=d;
        return arr[n];
    }
    if(n==4)
    {
        arr[n]=e;
        return arr[n];
    }
    if(n==5)
    {
        arr[n]=f;
        return arr[n];
    }
    if(arr[n]!=-1)
        return arr[n];
    arr[n]=F(n-1) + F(n-2) + F(n-3) + F(n-4) + F(n-5) + F(n-6);
    return arr[n];
}

int main()
{
    int cas;
    cin>>cas;

    for(int i=1; i<=cas; i++)
    {
        int n;
        cin>>a>>b>>c>>d>>e>>f>>n;
        for(int i=0;i<=n;i++)
            arr[I]=-1;
        long long int result=F(n)%10000007;
        count<<"Case "<<i<<": "<<result<<"\n";
    }
    return 0;
}

I got several wrong answers...

In the forum there is said: http://www.lightoj.com/forum_viewtopic.php?topic=193

A critical input output is:

Input:
1
10000007 0 0 0 0 0 0
Output:
Case 1: 0

I don't understand this... Can anyone help?? (edit: I got it... and edited the code. But still wrong answer. Can anybody tell me why?)[/I]
 
Last edited by a moderator:
Technology news on Phys.org
I need to login to view anything on that site.
 
Given a code (not optimized), and necessary inputs, you have to find the output of the code for the inputs. The code is as follows:

C:
int a, b, c, d, e, f;
int fn( int n ) {
    if( n == 0 ) return a;
    if( n == 1 ) return b;
    if( n == 2 ) return c;
    if( n == 3 ) return d;
    if( n == 4 ) return e;
    if( n == 5 ) return f;
    return( fn(n-1) + fn(n-2) + fn(n-3) + fn(n-4) + fn(n-5) + fn(n-6) );
}
int main() {
    int n, caseno = 0, cases;
    scanf("%d", &cases);
    while( cases-- ) {
        scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &n);
        printf("Case %d: %d[B]\n[/B]", ++caseno, fn(n) % 10000007);
    }
    return 0;
}

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains seven integers, a, b, c, d, e, f and n. All integers will be non-negative and 0 ≤ n ≤ 10000 and the each of the others will be fit into a 32-bit integer.

Output
For each case, print the output of the given code. The given code may have integer overflow problem in the compiler, so be careful.

Sample Input
Output for Sample Input
5

0 1 2 3 4 5 20

3 2 1 5 0 1 9

4 12 9 4 5 6 15

9 8 7 6 5 4 3

3 4 3 2 54 5 4

Case 1: 216339

Case 2: 79

Case 3: 16636

Case 4: 6

Case 5: 54
 
Last edited by a moderator:
I updated my code... It is

C:
#include <iostream>

using namespace std;

long long int arr[10001];
long long int a,b,c,d,e,f;

long long int F(int n)
{
    if(n==0)
    {
        arr[n]=a%10000007;
        return a;
    }
    if(n==1)
    {
        arr[n]=b%10000007;
        return arr[n];
    }
    if(n==2)
    {
        arr[n]=c%10000007;
        return arr[n];
    }
    if(n==3)
    {
        arr[n]=d%10000007;
        return arr[n];
    }
    if(n==4)
    {
        arr[n]=e%10000007;
        return arr[n];
    }
    if(n==5)
    {
        arr[n]=f%10000007;
        return arr[n];
    }
    if(arr[n]!=-1)
        return arr[n];
    arr[n]=(F(n-1)%10000007 + F(n-2)%10000007 + F(n-3)%10000007 + F(n-4)%10000007 + F(n-5)%10000007 + F(n-6)%10000007)%10000007;
    return arr[n];
}

int main()
{
    int cas;
    cin>>cas;

    for(int i=1; i<=cas; i++)
    {
        int n;
        cin>>a>>b>>c>>d>>e>>f>>n;
        for(int i=0;i<=n;i++)
            arr[i]=-1;
        long long int result=F(n);
        count<<"Case "<<i<<": "<<result<<"\n";
    }
    return 0;
}

but it is still getting W/A
 
Last edited by a moderator:
1. Please post the question.
2> Please use CODE tags.
 
  • Like
Likes   Reactions: berkeman
Vanadium 50 said:
1. Please post the question.
2> Please use CODE tags.
I posted the question
 
The question on the vjudge is:
Given a code (not optimized), and necessary inputs, you have to find the output of the code for the inputs. The code is as follows:

C:
int a, b, c, d, e, f;
int fn( int n ) {
if( n == 0 ) return a;
if( n == 1 ) return b;
if( n == 2 ) return c;
if( n == 3 ) return d;
if( n == 4 ) return e;
if( n == 5 ) return f;
return( fn(n-1) + fn(n-2) + fn(n-3) + fn(n-4) + fn(n-5) + fn(n-6) );
}
int main() {
int n, caseno = 0, cases;
scanf("%d", &cases);
while( cases-- ) {
scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &n);
printf("Case %d: %d[B]\n[/B]", ++caseno, fn(n) % 10000007);
}
return 0;
}

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains seven integers, a, b, c, d, e, f and n. All integers will be non-negative and 0 ≤ n ≤ 10000 and the each of the others will be fit into a 32-bit integer.

Output
For each case, print the output of the given code. The given code may have integer overflow problem in the compiler, so be careful.

Sample Input
Output for Sample Input

5

0 1 2 3 4 5 20

3 2 1 5 0 1 9

4 12 9 4 5 6 15

9 8 7 6 5 4 3

3 4 3 2 54 5 4

Case 1: 216339

Case 2: 79

Case 3: 16636

Case 4: 6

Case 5: 54
 
Last edited by a moderator:
It is accepted now :)

Thanks though
 
fireflies said:
I posted the question

No you didn't. You posted a link to a site not open to the general public.
 
  • #10
Vanadium 50 said:
No you didn't. You posted a link to a site not open to the general public.
I did... Check... I re-posted again. It is a virtual judge question, given the sample input and output... There is a code given there... We need to determine the possible outputs when the sample inputs are given. We cannot do it with the code given in the question since there is time limits 0.5 s. The question is here. Note, the code (not my one, the other one with sample input and output) is a part of the question.
 
  • #11
Vanadium 50 said:
No you didn't. You posted a link to a site not open to the general public.
Please read the first line of the post no#3 of the thread. That says what we have to do, and that post is the question :)
 
  • #12
Vanadium 50 said:
1. Please post the question.
2> Please use CODE tags.
fireflies said:
for(int i=0;i<=n;i++)
arr=-1;
long long int result=F(n);
You ignored V50's second point. Above is a quote of a few lines in your main() function. Below are the same lines of code. Notice a difference?
C:
for(int i=0;i<=n;i++)
            arr[i]=-1;
        long long int result=F(n);
Because you didn't use code tags, whatever indentation you might have used gets lost, making your code more difficult to read. This italics tag is also the reason that your code in post #4 suddenly switches to italics down near the end of your code. What's even worse, when you wrote arr, the browser renders the string as an italics BBCode tag, so arr becomes just arr, and now your code becomes truly unreadable. (I had to take extra steps to prevent the bracketed letter from being interpreted as an italics tag.)

For more information about code tags, see https://www.physicsforums.com/threads/read-this-before-posting-code-geshi-syntax-highlighter.773407/.
 
  • #14
Thanks for that :) Adding code tags is still hard for me. I hope I will learn soon. :)
 
  • #15
fireflies said:
Adding code tags is still hard for me.
They're not that hard at all. Put an opening code tag at the beginning, and a closing code tag at the bottom. In bold are the tags you would add for C code.
C:
[/B]int main() {
    int n, caseno = 0, cases;
    scanf("%d", &cases);
    while( cases-- ) {
        scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &n);
        printf("Case %d: %dn", ++caseno, fn(n) % 10000007);
    }
    return 0;
}
[B]

Here's how the above would appear in a browser (the browser consumes the code tags):
C:
int main() {
    int n, caseno = 0, cases;
    scanf("%d", &cases);
    while( cases-- ) {
        scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &n);
        printf("Case %d: %dn", ++caseno, fn(n) % 10000007);
    }
    return 0;
}
How hard is that?
fireflies said:
I hope I will learn soon. :)
Yes, I do too.
 
Last edited:
  • Like
Likes   Reactions: fireflies and berkeman
  • #16
Let's try

C:
int main()
{
printf("Thank You!");
return 0;
}
 
  • #17
Yeah! :)
 
  • #18
fireflies said:
Let's try

C:
int main()
{
printf("Thank You!");
return 0;
}
See? It's not hard.
Style-wise, I would indent the body of main() like this.
C:
int main()
{
   printf("Thank You!");
   return 0;
}

The code tag is pretty smart (or rather, the software that's behind these tags is). We support many different programming languages, so opening code tags with code=fortran or code=python will highlight keywords in those languages. Note that you can't have any spaces between 'code' and whatever language you're working with. There's a stickied post near the top of this forum section that goes into more detail about this stuff: https://www.physicsforums.com/threads/read-this-before-posting-code-geshi-syntax-highlighter.773407/.
 
  • Like
Likes   Reactions: fireflies
  • #19
I did not consider indentation actually. I might have been thinking the code tags does that too :D

And thanks for showing how to do this.
 
  • Like
Likes   Reactions: berkeman

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K