CHEFHACK - Editorial

@anton_lunyov (valid[i][j] is itself a array which has 1 at (i,j) if i,j lies inside the grid otherwise valid[i][j] contains 0) it is also workin well .please help to sort out this problem ,still unable to fiure out the problem

In evenserver() instead of

 if(!valid[k][l])
     return 0;

you should write

 if(k<0 || k>=n || l<0 || l>=n || !valid[k][l])
     return 0;

where n is the size of the grid that should be defined globally.

The same should be applied to oddserver() but be careful you are using variable n there as y-coordinate of the cell.

If you will have troubles with fixing it you can refer to my edit of your code that got AC:
http://www.codechef.com/viewsolution/1745787

Greatly appreciated, Anton! I should have noticed it by myself!

Use printf("%lld\n", tries).
I should add this tip to the editorial.
Several guys who asked for help all have this bug:
they use long long but print it as %d

thank you for the correction.
i tried another approach… facing a similar problem…

http://www.codechef.com/viewsolution/1747532

Wow! Your solution fails for only one grid among 80 grids we have in official test data. It is a grid having the following structure: N=350, a[i][j] are all even non-primes, except very small percent of odd non-primes. Each group of odd passwords is generated as a boundary of a small square.

Your solution is quite hard to follow so I can only suggest you to generate test of the above structure for smaller size and compare your answer with the answer generated by the correct program.

Hi Anton,
Is (1,1) connected to (1,4)? I don’t seem to understand the connectivity rules to get to answer 2.
Thank you for the small test case.
-nnovoice

Hi Anton, I was checking this link: CHEFHACK compiler - general - CodeChef Discuss and you have posted the same test case with 12 as the answer!
-nnovoice

Sorry, I’ve swapped the correct and yours answers :stuck_out_tongue:
Is this clear your doubt?

approach is as :

  1. generate prime numbers index.
  2. read data (simultaneously add attempts for primes). store odd/even flag for remaining.
  3. get_neighbors() will store ‘valid’ neighbors for each data. ‘valid’ means - (i) it is either [i+1][j] or [i][j+1]. (ii) it has to be of same type (odd/even) non-primes.
  4. ‘tags[i][j]’ for each member is assigned as ‘i*N+j’.
  5. arrange_and_compute() will do traversal over all members and update ‘valid’ neighbors with lowest tags among them. this is done in loop until there is no change in an iteration.

this was an attempt on amortization. will see!

Thank you. BTW, that was not my answer! -nnovoice

You should not pass through the cells containing 2 in mark_check_even. Try this test:

4
0 4 2 12
2 1 9 24
0 1 9 12
2 8 2 18 

The answer is 12 while your answer is 2.

Thanks a lot…

Hi Anton,
Would you be able to help me in finding why my solution fails with SIGSEGV: CodeChef: Practical coding for everyone? If you could help me with a test case, I will try to find out. I have tried to find a problem in the code without success.
Thank you.
-nnovoice

The reason is stack overflow in DFS.
Try to get rid of arrays rows[] and cols[].
For example, you could make them vectors.
Then they will be allocated not in stack.

1 Like

Thank you Anton! I will try that.

I tried vector but they slowed down the execution. So, I went with allocating memory on the heap using new. Submitted with correct answer! Thank you!

Very nice editorial @anton_lunyov
I loved the fact that you right away told the people the test cases on which their code was going south. I miss that in other editorials. I like codeforces as they show us the test cases where one’s code goes wrong and I really miss this in codechef.
Also you gave the links of sample problems and I very much liked this gesture of yours.

1 Like

Hey,Can Some one check my solution.It does not pass sys test.

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define lld long double
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define precise(ans)  cout<<fixed<<setprecision(15)<<ans
typedef pair<int, int>  pii;
typedef pair<ll, ll>    pl;
typedef vector<int>     vi;
typedef vector<vi>      vvi;
typedef vector<vvi>    vvvi;
typedef vector<ll>      vl;
typedef vector<vl>      vvl;
typedef vector<pii>     vpii;
typedef vector<pl>      vpl;
const int mod = 1000000007;
#define PI 3.1415926535897932384626
const int M=1e9+7;
const int N=1e7+10;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vector<bool> prime(N, true);
vector<ll> aa(N,0);
void primesieve(){
    prime[0] = false;
    prime[1] = false;
    for (int i = 2; i * i <N; i++)
    {
        if (prime[i] == true)
        {
            aa[i]=1;
            for (int j = i * i; j < N; j = j + i)
            {
                prime[j] = false;
            }
        }
    }
    for(ll i=2;i<N;i++){
      aa[i]+=aa[i-1];
    }
}
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};

void dfs(vector<vector<ll>>& bb,int i,int j,int n,vector<vector<int>>& vis){
  for(ll k=0;k<4;k++){
    ll x=i+dx[k],y=j+dy[k];
    if(x>=0 && y>=0 && x<n && y<n && vis[x][y]==0 && (!prime[bb[x][y]])){
     if((bb[i][j]&1)==(bb[x][y]&1)){
        vis[x][y]=1;
        dfs(bb,x,y,n,vis);
      }
    }
  }
}

void solve() {
  ll n;
  cin>>n;
  vector<vector<ll>> bb(n,vector<ll>(n));
  vector<vector<int>> vis(n,vector<int>(n,0));
  for(ll i=0;i<n;i++){
    for(ll j=0;j<n;j++){
      cin>>bb[i][j];
    }
  }
  
  ll ans=0;
  for(ll i=0;i<n;i++){
    for(ll j=0;j<n;j++){
      if(vis[i][j]==1){
        cout<<"-1 ";
        continue;
      }
      vis[i][j]=1;
      if(prime[bb[i][j]]){
        cout<<(aa[bb[i][j]]-1)<<" ";
        ans+=(aa[bb[i][j]]-1);
      }else{
        if(bb[i][j]%2==0){
          cout<<(bb[i][j]/2)<<" ";
          ans+=(bb[i][j]/2);
        }else{
          cout<<((bb[i][j]+1)/2)+1<<" ";
          ans+=((bb[i][j]+1)/2)+1;
        }
        dfs(bb,i,j,n,vis);
      }
    }
    cout<<endl;
  }
  cout<<ans<<endl;

}


int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  srand(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll  t;
  t = 1;
  cin >> t;
  primesieve();
  while (t--) {
    solve();

  }

  return 0;
}

Does anyone know what code this is
+221 and #3 is important
Code is used to generate fake numbers and hide real ones

+221 76 324 94 53

0469 884 738 Correct number is 0466 424 748
0472 709 087 correct number unknown
0473 140 950 correct number unknown
0473 545 385 correct number unknown
0474 035 470 correct number unknown
0474 206 394 correct number unknown
0474 858 013 correct number unknown
0475 145 637 correct number is 0475 361 745