Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created December 28, 2016 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/5525a479e947d2d3e8196c51649ebd0b to your computer and use it in GitHub Desktop.
Save jianminchen/5525a479e947d2d3e8196c51649ebd0b to your computer and use it in GitHub Desktop.
Hackerrank - week code 27 - Hackonacci matrix rotation - study C++ code - Google employee's code
#include <iostream>
#include <cassert>
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <time.h>
#include <set>
#define ll long long
using namespace std;
bool matpow(int numdims, ll N)
{
if (N%7==4) {
return 1;
}
if (N%7==5) {
return 1;
}
if (N%7==6) {
return 0;
}
if (N%7==0) {
return 1;
}
if (N%7==1) {
return 0;
}
if (N%7==2) {
return 0;
}
if (N%7==3) {
return 1;
}
return false;
}
bool GetNumber(ll x) {
if (x==1)
return 1;
if (x==2)
return 0;
if (x==3)
return 1;
return matpow(3, x-3);
}
bool A[2000][2000];
bool B[2000][2000];
bool tmp2[2000][2000];
int ans[4];
void Count(int n) {
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
B[i][j]=A[i][j];
ans[0]=0;
for(int rot=1;rot<=3;rot++) {
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
tmp2[j][n-1-i]=B[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++) {
B[i][j]=tmp2[i][j];
}
ans[rot]=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
ans[rot]+=(B[i][j]!=A[i][j]);
}
}
int main() {
int n,Q;
cin>>n>>Q;
for(ll i=0;i<n;i++)
for(ll j=0;j<n;j++) {
if (GetNumber((i+1)*(i+1)*(j+1)*(j+1)))
A[i][j]=1;
else
A[i][j]=0;
}
Count(n);
for(int q=1;q<=Q;q++) {
int ang;
cin>>ang;
cout << ans[(ang%360)/90] << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment