644A - Parliament of Berland - CodeForces Solution


*special problem constructive algorithms *1000

Please click on ads to support us..

Python Code:

import math
from collections import deque


alfabet = {'a': 1, 'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
rasp=''
for i in range(0,1):
 n,a,b=map(int,input().split())
    
    cd=1
 cr=2
 ct=0
 
 if n>a*b:
  print(-1)
 else: 
  for i in range(0,a):
   for j in range(0,b):
    if (i&1)==(j&1):
     if cd<=n:
      print(cd,end=' ')
      cd+=2
     else:
      print(0, end=' ')
    else:
      if cr<=n:
       print(cr,end=' ')
       cr+=2
      else:
       print(0, end=' ')
   print()
 
 

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
ll n,a,b; cin>>n>>a>>b;
if(a*b<n){
    cout<<"-1"; return 0;
}
ll s[a][b]={0};
for(ll i=0;i<a;i++){
for(ll j=0;j<b;j++){
    s[i][j]=0;
}
}
ll i=0,j=0,k=0,r=1;
while(1){
    s[i][j]=r;
    if(r==n) break;
    else r++;

    if(k==0) j++;
    if(k==1) j--;
    if(j==b){
        j--; i++; k=1;
    }
    if(j==-1){
        j++; i++; k=0;
    }
}
for(ll i=0;i<a;i++){
for(ll j=0;j<b;j++){
    cout<<s[i][j]<<" ";
}
cout<<endl;
}
}


Comments

Submit
0 Comments
More Questions

811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort