简体   繁体   中英

Getting WA for SPOJ ANDROUND (5832)

I am trying out this problem: http://www.spoj.com/problems/ANDROUND

My algo makes of 2D array of 32 rows and N columns (32 because after 31 rounds, the array doesn't change). It then calculates each field in the 2D array by taking the AND of the value in the previous row (column remaining same), and its neighbours. I have checked with almost all sorts of input and have got the desired output. But still getting WA. Here is my code. If anybody could point out the error or give a test case for which my program fails.

#include <cstdio>
#include <cstring>
using namespace std;

static long A[33][20002], N, T, K;

int main()
{
    scanf("%ld", &T);
    while(T--){
        memset(A, 0, sizeof A);
        scanf("%ld %ld", &N, &K); K=(K>31)? 31:K;                       \\Setting K=31 if K>31
        for(int i=1; i<=N; ++i) scanf("%ld", &A[0][i]);
        A[0][0]=A[0][N]; A[0][N+1]=A[0][1];                                \\first row is the input array

        for(int i=1; i<=K; ++i){
            for(int j=1; j<=N; ++j)
                A[i][j]= (A[i-1][j]&A[i-1][j-1]) & A[i-1][j+1];  \\taking AND of previous element in column and its neighbors.

            A[i][0]=A[i][N]; A[i][N+1]=A[i][1];           \\for making array cyclic.
        }
        for (int i=1; i<=N; ++i) printf("%ld ", A[K][i]);                     \\Printing the array after required rounds.
        printf("\n");
    }
    return 0;
}

Thank you.

because after 31 rounds, the array doesn't change

What makes you believe that?

After R rounds, the element at index i is only influenced by the elements

A[i-R] ... A[i+R]

(with suitable wrapping if i <= R or N < i+R ).

So it is only guaranteed that the array remains unchanged after N/2 rounds.

Try this test case.

1 500 1000000000 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM