Problem:
Problem A:
Moduic Squares
Have you ever heard of Moduic Squares? They are like 3 × 3 Magic Squares, but each of them has one
extra cell called a moduic cell. Hence a Moduic Square has the following form.
Figure 1: A Moduic Square
Each of cells labeled from A to J contains one number from 1 to 10, where no two cells contain the same
number. The sums of three numbers in the same rows, in the same columns, and in the diagonals in the
3 × 3 cells must be congruent modulo the number in the moduic cell. Here is an example Moduic Square:
Figure 2: An example Moduic Square
You can easily see that all the sums are congruent to 0 modulo 5.
Now, we can consider interesting puzzles in which we complete Moduic Squares with partially filled cells.
For example, we can complete the following square by filling 4 into the empty cell on the left and 9 on
the right. Alternatively, we can also complete the square by filling 9 on the left and 4 on the right. So
this puzzle has two possible solutions.
Figure 3: A Moduic Square as a puzzle
Your task is to write a program that determines the number of solutions for each given puzzle.
Input
The input contains multiple test cases. Each test case is specified on a single line made of 10 integers
that represent cells A, B, C, D, E, F, G, H, I, and J as shown in the first figure of the problem statement.
Positive integer means that the corresponding cell is already filled with the integer. Zero means that the
corresponding cell is left empty.
The end of input is identified with a line containing ten of -1’s. This is not part of test cases.
Output
For each test case, output a single integer indicating the number of solutions on a line. There may be
cases with no solutions, in which you should output 0 as the number.
Sample Input
3 1 6 8 10 7 0 0 2 5
0 0 0 0 0 0 0 0 0 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Output for the Sample Input
2
362880

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int a[3][3], m, ans;
bool chk[11];

void f(int p) {
	if (p == 10) {
		set<int> num, mod;
		num.insert(m);
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) num.insert(a[i][j]);
			mod.insert((a[i][0] + a[i][1] + a[i][2]) % m);
			mod.insert((a[0][i] + a[1][i] + a[2][i]) % m);
		}
		mod.insert((a[0][0] + a[1][1] + a[2][2]) % m);
		mod.insert((a[0][2] + a[1][1] + a[2][0]) % m);
		if (num.size() == 10 && mod.size() == 1) ans++;
		return;
	}
	if (p == 9) {
		if (m > 0) f(p + 1);
		else {
			for (int i = 1; i <= 10; i++) {
				if (chk[i]) continue;
				m = i;
				f(p + 1);
				m = 0;
			}
		}
	}
	else {
		int r = p / 3, c = p % 3;
		if (a[r][c] > 0) f(p + 1);
		else {
			for (int i = 1; i <= 10; i++) {
				if (chk[i]) continue;
				chk[i] = true, a[r][c] = i;
				f(p + 1);
				chk[i] = false, a[r][c] = 0;
			}
		}
	}
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(0);

	while (true) {
		memset(chk, 0, sizeof(chk));
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				cin >> a[i][j];
				if (a[i][j] > 0) chk[a[i][j]] = true;
			}
		}
		cin >> m; if (m == -1) break;
		if (m) chk[m] = true;

		ans = 0; f(0);
		cout << ans << '\n';
	}
	return 0;
}
accuracy: 1
time_ms: 2740
memory_kb: 3124
final_rank: 18

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    vector<vector<int>> v;
    vector<int> x(10);
    for(int i = 0; i < 10; i++) x[i] = i+1;
    do{
        int z = (x[0]+x[1]+x[2])%x[9];
        bool judge = true;
        for(int i = 0; i < 3; i++){
            judge &= (x[3*i]+x[3*i+1]+x[3*i+2])%x[9] == z;
            judge &= (x[i]+x[3+i]+x[6+i])%x[9] == z;
        }
        judge &= (x[0]+x[4]+x[8])%x[9] == z;
        judge &= (x[2]+x[4]+x[6])%x[9] == z;
        if(judge)   v.push_back(x);
    }while(next_permutation(x.begin(),x.end()));
    
    while(1){
        for(int i = 0; i < 10; i++) cin >> x[i];
        if(x[0] == -1)  break;
        int ans = 0;
        for(vector<int> y : v){
            bool judge = true;
            for(int i = 0; i < 10; i++){
                judge &= x[i]==0 || x[i]==y[i];
            }
            if(judge)   ans++;
        }
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 28348
final_rank: 19

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;


int main() {
	vector<int>v(10);
	iota(v.begin(),v.end(),1);

	vector<vector<int>>anss;
	do {
		vector<vector<int>>field(3,vector<int>(3));
		const int mod=v[9];
		for (int i = 0; i < 3; ++i) {
			for (int j = 0; j < 3; ++j) {
				field[i][j]=v[i*3+j];
			}
		}
		bool ok=true;
		int need=field[0][0]+field[1][1]+field[2][2];
		need%=mod;
		for (int y = 0; y < 3; ++y) {
			int asum=0;
			for (int x = 0; x < 3; ++x) {
				asum+=field[y][x];
			}
			if(asum%mod!=need)ok=false;
		}
		for (int x = 0; x < 3; ++x) {
			int asum=0;
			for (int y = 0; y < 3; ++y) {
				asum+=field[y][x];
			}
			if(asum%mod != need)ok=false;
		}
		for (int i = 0; i < 2; ++i) {
			int asum=0;
			for (int j = 0; j < 3; ++j) {
				asum+=field[j][i?j:2-j];
			}
			if(asum%mod != need)ok=false;
		}
		if (ok) {
			anss.emplace_back(v);
		}
	}while(next_permutation(v.begin(),v.end()));

	while (true){
		vector<int>v(10);
		for(int i=0;i<10;++i)cin>>v[i];
		if(v[0]==-1)break;
		int sum=0;
		for (auto && ans : anss) {
			bool ok=true;
			for (int i = 0; i < 10; ++i) {
				if(v[i]&&v[i]!=ans[i])ok=false;
			}
			if(ok)sum++;
		}
		cout<<sum<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 580
memory_kb: 28260
final_rank: 20

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	int table[3][3];
	int mod_num,used,decided_num;
};

int POW[11];

int ans;

bool check(Info info){

	bool check_table[10];
	for(int i = 0; i < 10; i++)check_table[i] = false;
	int row_0_num,row_1_num,row_2_num,col_0_num,col_1_num,col_2_num,cross_0_num,cross_1_num;

	if(info.table[0][0] == 0 || info.table[0][1] == 0 || info.table[0][2] == 0){

	}else{
		row_0_num = (info.table[0][0]+info.table[0][1]+info.table[0][2])%info.mod_num;
		check_table[row_0_num] = true;
	}

	if(info.table[1][0] == 0 || info.table[1][1] == 0 || info.table[1][2] == 0){

	}else{
		row_1_num = (info.table[1][0]+info.table[1][1]+info.table[1][2])%info.mod_num;
		check_table[row_1_num] = true;
	}

	if(info.table[2][0] == 0 || info.table[2][1] == 0 || info.table[2][2] == 0){

	}else{
		row_2_num = (info.table[2][0]+info.table[2][1]+info.table[2][2])%info.mod_num;
		check_table[row_2_num] = true;
	}

	if(info.table[0][0] == 0 || info.table[1][0] == 0 || info.table[2][0] == 0){

	}else{
		col_0_num = (info.table[0][0]+info.table[1][0]+info.table[2][0])%info.mod_num;
		check_table[col_0_num] = true;
	}

	if(info.table[0][1] == 0 || info.table[1][1] == 0 || info.table[2][1] == 0){

	}else{
		col_1_num = (info.table[0][1]+info.table[1][1]+info.table[2][1])%info.mod_num;
		check_table[col_1_num] = true;
	}

	if(info.table[0][2] == 0 || info.table[1][2] == 0 || info.table[2][2] == 0){

	}else{
		col_2_num = (info.table[0][2]+info.table[1][2]+info.table[2][2])%info.mod_num;
		check_table[col_2_num] = true;
	}

	if(info.table[0][0] == 0 || info.table[1][1] == 0 || info.table[2][2] == 0){

	}else{
		cross_0_num = (info.table[0][0]+info.table[1][1]+info.table[2][2])%info.mod_num;
		check_table[cross_0_num] = true;
	}

	if(info.table[0][2] == 0 || info.table[1][1] == 0 || info.table[2][0] == 0){

	}else{
		cross_1_num = (info.table[0][2]+info.table[1][1]+info.table[2][0])%info.mod_num;
		check_table[cross_1_num] = true;
	}

	int count = 0;
	for(int i = 0; i < 10; i++){
		if(check_table[i]){
			count++;
		}
	}

	return count <= 1;

}

void copyInfo(Info& to,Info from){
	for(int row = 0; row < 3; row++){
		for(int col = 0; col < 3; col++){
			to.table[row][col] = from.table[row][col];
		}
	}
	to.mod_num = from.mod_num;
}

void recursive(Info info){

	if(info.decided_num == 10){

		if(check(info)){
			ans++;
		}
		return;
	}


	if(info.mod_num == 0){
		for(int loop = 1; loop <= 10; loop++){
			if(info.used & (1 << loop)){
				//Do nothing
			}else{
				Info new_info;
				copyInfo(new_info,info);
				new_info.mod_num = loop;
				new_info.decided_num = info.decided_num+1;
				new_info.used = info.used + POW[loop];
				if(check(new_info)){
					recursive(new_info);
				}
			}
		}
		return;
	}

	for(int row = 0; row < 3; row++){
		for(int col = 0; col < 3; col++){
			if(info.table[row][col] == 0){
				for(int loop = 1; loop <= 10; loop++){
					if(info.used & (1 << loop)){
						//Do nothing
					}else{
						Info new_info;
						copyInfo(new_info,info);
						new_info.table[row][col] = loop;
						new_info.decided_num = info.decided_num+1;
						new_info.used = info.used + POW[loop];
						if(check(new_info)){
							recursive(new_info);
						}
					}
				}
				return;
			}
		}
	}
}


int main(){

	for(int i = 1; i <= 10; i++)POW[i] = pow(2,i);

	while(true){
		Info first;
		for(int row = 0; row < 3; row++){
			for(int col = 0; col < 3; col++)scanf("%d",&first.table[row][col]);
		}
		scanf("%d",&first.mod_num);

		if(first.table[0][0] == -1)break;

		first.used = 0;
		first.decided_num = 0;

		for(int row = 0; row < 3; row++){
			for(int col = 0; col < 3; col++){
				if(first.table[row][col] != 0){
					first.used += POW[first.table[row][col]];
					first.decided_num++;
				}
			}
		}

		if(first.mod_num != 0){
			first.used += POW[first.mod_num];
			first.decided_num++;
		}

		ans = 0;

		recursive(first);

		printf("%d\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3316
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
int t[9],d;
bool used[11];
bool C(void){
  int r=(t[0]+t[1]+t[2])%t[9];
  return ((t[3]+t[4]+t[5])%t[9]==r&&(t[6]+t[7]+t[8])%t[9]==r&&(t[0]+t[3]+t[6])%t[9]==r&&(t[1]+t[4]+t[7])%t[9]==r&&(t[2]+t[5]+t[8])%t[9]==r&&(t[0]+t[4]+t[8])%t[9]==r&&(t[2]+t[4]+t[6])%t[9]==r);
}
 
int dfs(int d){
  if(d==10)return C()?1:0;
  if(t[d]!=0)return dfs(d+1);
  int res = 0;
  for(int i=1;i<=10;i++){
    if(!used[i]){
      t[d]=i;
      used[i]=1;
      res+=dfs(d+1);
      used[i]=0;
      t[d]=0;
    }
  }
  return res;
}
main(){
  while(1){
    memset(used,0,sizeof(used));
    for(int i=0;i<10;i++){
      cin>>t[i];
      if(t[i]>0)used[t[i]]=1;
    }
    if(t[0]==-1)break;
    cout<<dfs(0)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3040
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
int t[9],d;
bool used[11];
bool C(void){
  int r=(t[0]+t[1]+t[2])%t[9];
  return ((t[3]+t[4]+t[5])%t[9]==r&&(t[6]+t[7]+t[8])%t[9]==r&&(t[0]+t[3]+t[6])%t[9]==r&&(t[1]+t[4]+t[7])%t[9]==r&&(t[2]+t[5]+t[8])%t[9]==r&&(t[0]+t[4]+t[8])%t[9]==r&&(t[2]+t[4]+t[6])%t[9]==r);
}
 
int dfs(int d){
  if(d==10)return C()?1:0;
  if(t[d]!=0)return dfs(d+1);
  int res = 0;
  for(int i=1;i<=10;i++){
    if(!used[i]){
      t[d] = i;
      used[i] = true;
      res+=dfs(d+1);
      used[i] = false;
      t[d] = 0;
    }
  }
  return res;
}
main(){
  while(1){
    memset(used,0,sizeof(used));
    for(int i=0;i<10;i++){
      cin>>t[i];
      if(t[i]>0)used[t[i]]=1;
    }
    if(t[0]==-1)break;
    cout<<dfs(0)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 2964
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
bool u[11];
int a[10],x,y;
bool ck() {
  if(a[0]&&a[1]&&a[2]&&(a[0]+a[1]+a[2])%x!=y) return 0;
  if(a[3]&&a[4]&&a[5]&&(a[3]+a[4]+a[5])%x!=y) return 0;
  if(a[6]&&a[7]&&a[8]&&(a[6]+a[7]+a[8])%x!=y) return 0;
  if(a[0]&&a[3]&&a[6]&&(a[0]+a[3]+a[6])%x!=y) return 0;
  if(a[1]&&a[4]&&a[7]&&(a[1]+a[4]+a[7])%x!=y) return 0;
  if(a[2]&&a[5]&&a[8]&&(a[2]+a[5]+a[8])%x!=y) return 0;
  if(a[0]&&a[4]&&a[8]&&(a[0]+a[4]+a[8])%x!=y) return 0;
  if(a[2]&&a[4]&&a[6]&&(a[2]+a[4]+a[6])%x!=y) return 0;
  return 1;
}
 
int dfs(int k) {
  if(!ck()) return 0;
  if(k==9) return 1;
  int ans=0;
  if(a[k]) ans+=dfs(k+1);
  else {
    for(int i=1; i<=10; i++) {
      if(u[i]) continue;
      a[k]=i;u[i]=1;
      ans+=dfs(k+1);
      a[k]=0;u[i]=0;
    }
  }
  return ans;
}
 
int main() {
  while(1) {
    memset(u,0,sizeof(u));
    for(int i=0; i<10; i++) {
      cin >> a[i];
      u[a[i]]=1;
    }
    if(a[0]==-1) break;
    x=a[9];
    int ans=0;
    if(!x) {
      for(int i=1; i<11; i++) {
        if(u[i]) continue;
        x=i;
        u[i]=1;
        for(int j=0; j<x; j++) {y=j;ans+=dfs(0);}
        u[i]=0;
      }
    } else {
      for(int j=0; j<x; j++) {y=j;ans+=dfs(0);}
    }
    cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3092
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#define index ffff
using namespace std;

int d[10],ans;
int index[10];
int M;

int cal(int a,int b,int c){
  
  return (index[a]+index[b]+index[c])%M;
  
}


bool check(){
  
  M=index[9];
  
  int X=cal(0,1,2);
  
  if(X!=cal(3,4,5))return false;
  
  if(X!=cal(6,7,8))return false;

  if(X!=cal(0,3,6))return false;

  if(X!=cal(1,4,7))return false;
  
  if(X!=cal(2,5,8))return false;
  
  if(X!=cal(0,4,8))return false;
  
  if(X!=cal(2,4,6))return false;

  return true;
  
}

void solve(){
  

  for(int i=0;i<10;i++) index[i]=i+1;


  do{
    int f=0;
    
    for(int i=0;i<10;i++)
      if(d[i]!=0&&d[i]!=index[i]){
	f=1;
	break;
      }

    if(f)continue;
    
    if(check())ans++;
        
  }while(next_permutation(index,index+10));
  
}


int main(){

  while(1){
    
    for(int i=0;i<10;i++) cin>>d[i];
    
    if(d[0]<0)break;

    ans=0;
    
    solve();
    
    cout<<ans<<endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 390
memory_kb: 3016
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
bool u[11];
int a[10],x,y;
bool ck() {
  if(a[0]&&a[1]&&a[2]&&(a[0]+a[1]+a[2])%x!=y) return 0;
  if(a[3]&&a[4]&&a[5]&&(a[3]+a[4]+a[5])%x!=y) return 0;
  if(a[6]&&a[7]&&a[8]&&(a[6]+a[7]+a[8])%x!=y) return 0;
  if(a[0]&&a[3]&&a[6]&&(a[0]+a[3]+a[6])%x!=y) return 0;
  if(a[1]&&a[4]&&a[7]&&(a[1]+a[4]+a[7])%x!=y) return 0;
  if(a[2]&&a[5]&&a[8]&&(a[2]+a[5]+a[8])%x!=y) return 0;
  if(a[0]&&a[4]&&a[8]&&(a[0]+a[4]+a[8])%x!=y) return 0;
  if(a[2]&&a[4]&&a[6]&&(a[2]+a[4]+a[6])%x!=y) return 0;
  return 1;
}

int dfs(int k) {
  if(!ck()) return 0;
  if(k==9) return 1;
  int ans=0;
  if(a[k]) ans+=dfs(k+1);
  else {
    for(int i=1; i<=10; i++) {
      if(u[i]) continue;
      a[k]=i;
      u[i]=1;
      ans+=dfs(k+1);
      a[k]=0;
      u[i]=0;
    }
  }
  return ans;
}

int main() {
  while(1) {
    memset(u,0,sizeof(u));
    for(int i=0; i<10; i++) {
      cin >> a[i];
      u[a[i]]=1;
    }
    if(a[0]==-1) break;
    x=a[9];
    int ans=0;
    if(!x) {
      for(int i=1; i<11; i++) {
        if(u[i]) continue;
        x=i;
        u[i]=1;
        for(int j=0; j<x; j++) {
          y=j;
          ans+=dfs(0);
        }
        u[i]=0;
      }
    } else {
      for(int j=0; j<x; j++) {
        y=j;
        ans+=dfs(0);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3096
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
int diff[10];
int num[]={1,2,3,4,5,6,7,8,9,10};

bool check(){
  int A[3][3],mod = num[9];
  for(int i=0;i<10;i++)if(diff[i]>0&&num[i]!=diff[i])return 0;
  for(int i=0;i<9;i++) A[i/3][i%3] = num[i];


  int used[20]={};
  for(int i=0;i<3;i++){
    int a=0,b=0;    
    for(int j=0;j<3;j++)a+=A[i][j],b+=A[j][i];
    a%=mod,b%=mod;
    used[a]++;
    used[b]++;
    if(used[a]!=(i+1)*2)return 0;    
  }
  
int a =A[0][0]+A[1][1]+A[2][2],b = A[0][2]+A[1][1]+A[2][0];
used[a%mod]++,used[b%mod]++;
 return used[a%mod] == 8;
}

signed main(){

  while(1){
    int Exit = 1;
    for(int i=0;i<10;i++)cin>>diff[i],Exit*=(diff[i]<0);
    if(Exit) return 0;

  for(int i=0;i<10;i++)num[i] = i+1;

  int ans=0;
  do{
    ans+=check();
  }while(next_permutation(num,num+10));
  cout<<ans<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 570
memory_kb: 3032
final_rank: 15

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int m;
vi tmp;
int w[8][3]={
	{0,1,2},
	{3,4,5},
	{6,7,8},
	{0,3,6},
	{1,4,7},
	{2,5,8},
	{0,4,8},
	{2,4,6}
};
vi in;
bool f(){
	vi su(8);
	rep(i,8){
		int sum=0;
		rep(j,3)sum+=tmp[w[i][j]];
		su[i]=sum%m;
	}
	bool h=true;
	rep(i,8)if(su[0]!=su[i])h=false;
	return h;
}
int main(){
	while(1){
		in=vi(10);
		rep(i,10)cin>>in[i];
		if(in[0]==-1)break;
		tmp=vi(11);
		rep(i,10)tmp[in[i]]++;
		vi per;
		rep(i,10)if(tmp[i+1]==0)per.pb(i+1);
		int out=0;
		do{
			tmp=in;
			int c=0;
			rep(i,10)if(tmp[i]==0)tmp[i]=per[c],c++;
			m=tmp[9];
			out+=f();
		}while(next_permutation(all(per)));
		cout<<out<<endl;
	}
}
accuracy: 1
time_ms: 430
memory_kb: 1216
final_rank: 12

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
#define rep(i,n) for(int i = 0 ; i < n ; i++)
 
int c[10];
 
bool check(){
    int sum = 0, MOD = c[9];
    rep(i,3) sum = (sum + c[i]) % MOD; 
    if((c[3]+c[4]+c[5])%MOD != sum) return false;
    if((c[6]+c[7]+c[8])%MOD != sum) return false;
    if((c[0]+c[3]+c[6])%MOD != sum) return false;
    if((c[1]+c[4]+c[7])%MOD != sum) return false;
    if((c[2]+c[5]+c[8])%MOD != sum) return false;
    if((c[0]+c[4]+c[8])%MOD != sum) return false;
    if((c[2]+c[4]+c[6])%MOD != sum) return false;
    return true;
}
 
int rec(int p,int S){
    int res = 0;
    if(__builtin_popcount(S) == 10){
	return check();
    }
    if(!c[p]){
	rep(i,10){
	    if((S >> i) & 1) continue;
	    c[p] = i+1;
	    res += rec(p+1,S|(1<<i));
	    c[p] = 0;
	}
    }else{
	res += rec(p+1,S);
    }
    return res;
}
 
int main(){
    while(true){
	int S = 0;
	bool EXIT = false;
	rep(i,10){
	    cin >> c[i];
	    if(c[i] > 0){
		S |= 1<<(c[i]-1);
	    }else if(c[i] == -1){
		EXIT = true;
	    }
	}
	if(EXIT) break;
	cout << rec(0,S) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 3048
final_rank: 11

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int a[10];
int main() {
	while (true) {
		for (int i = 0; i < 10; i++)cin >> a[i];
		if (a[0] == -1)break;
		int b[10] = { 0,1,2,3,4,5,6,7,8,9 }; int cnt = 0;
		do {
			int OK = 1; int f[10];
			for (int i = 0; i < 10; i++)f[i] = b[i] + 1;
			for (int i = 0; i < 10; i++) {
				if (a[i] >= 1 && a[i] != f[i])OK = 0;
			}
			if (OK == 0)continue;
			int c[4][4];
			for (int i = 0; i < 16; i++)c[i / 4][i % 4] = 0;
			for (int i = 0; i < 9; i++) {
				c[i / 3][i % 3] = f[i];
				c[3][i % 3] += f[i]; c[3][i % 3] %= f[9];
				c[i / 3][3] += f[i]; c[i / 3][3] %= f[9];
			}
			if (c[3][0] != c[3][1] || c[3][1] != c[3][2])continue;
			if (c[0][3] != c[1][3] || c[1][3] != c[2][3])continue;
			int d1 = c[0][0] + c[1][1] + c[2][2]; d1 %= f[9];
			int d2 = c[0][2] + c[1][1] + c[2][0]; d2 %= f[9];
			if (c[3][0] != d1 || d1 != d2)continue;
			cnt++;
		} while (next_permutation(b, b + 10));
		cout << cnt << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 1570
memory_kb: 1160
final_rank: 17

code_snippet: #include <cstdio>
int a[10], e[8], used[11];
bool ok() {
	for (int i = 0; i < 3; i++) e[i] = (a[i] + a[i + 3] + a[i + 6]) % a[9];
	for (int i = 0; i < 3; i++) e[i + 3] = (a[i * 3] + a[i * 3 + 1] + a[i * 3 + 2]) % a[9];
	e[6] = (a[2] + a[4] + a[6]) % a[9];
	e[7] = (a[0] + a[4] + a[8]) % a[9];
	for (int i = 0; i < 7; i++) if (e[i] != e[i + 1]) return false;
	return true;
}
int solve(int depth) {
	if (depth == 10) return ok() ? 1 : 0;
	if (a[depth]) return solve(depth + 1);
	int ret = 0;
	for (int i = 1; i <= 10; i++) {
		if (!used[i]) {
			used[i] = 1; a[depth] = i;
			ret += solve(depth + 1);
			used[i] = a[depth] = 0;
		}
	}
	return ret;
}
int main() {
	while (true) {
		for (int i = 1; i < 11; i++) used[i] = 0;
		for (int i = 0; i < 10; i++) {
			scanf("%d", a + i);
			if (a[i]) used[a[i]] = 1;
		}
		if (a[0] == -1) break;
		printf("%d\n", solve(0));
	}
}
accuracy: 1
time_ms: 190
memory_kb: 2604
final_rank: 9

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

/* typedef */

/* global variables */

int ds[10];
bool used[11];

/* subroutines */

inline int dsum(int i0, int i1, int i2) {
  return (ds[i0] + ds[i1] + ds[i2]) % ds[9];
}

bool check() {
  int diag0 = dsum(0, 4, 8);
  return
    dsum(2, 4, 6) == diag0 &&
    dsum(0, 1, 2) == diag0 &&
    dsum(3, 4, 5) == diag0 &&
    dsum(6, 7, 8) == diag0 &&
    dsum(0, 3, 6) == diag0 &&
    dsum(1, 4, 7) == diag0 &&
    dsum(2, 5, 8) == diag0;
}

int rec(int k) {
  if (k >= 10) return check();

  if (ds[k] == 0) {
    int sum = 0;
    for (int d = 1; d <= 10; d++)
      if (! used[d]) {
	ds[k] = d;
	used[d] = true;
	sum += rec(k + 1);
	ds[k] = 0;
	used[d] = false;
      }
    return sum;
  }

  return rec(k + 1);
}

/* main */

int main() {
  for (;;) {
    cin >> ds[0];
    if (ds[0] == -1) break;

    for (int i = 1; i < 10; i++) cin >> ds[i];

    memset(used, false, sizeof(used));

    for (int i = 0; i < 10; i++)
      if (ds[i] > 0) used[ds[i]] = true;

    cout << rec(0) << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 1160
final_rank: 4

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <complex>
#include <assert.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <double,double> P;
 
static const int tx[] = {+0,+1,+0,-1};
static const int ty[] = {-1,+0,+1,+0};
 
static const double EPS = 1e-8;

int table[3][3];

bool check(int mod){
  set<int> result;
  for(int y = 0; y < 3; y++){
    int sum = 0;
    for(int x = 0; x < 3; x++){
      sum += table[y][x];
    }
    result.insert(sum % mod);
  }
  if(result.size() > 1) return false;

  for(int x = 0; x < 3; x++){
    int sum = 0;
    for(int y = 0; y < 3; y++){
      sum += table[y][x];
    }
    result.insert(sum % mod);
  }
  if(result.size() > 1) return false;

  result.insert((table[0][0] + table[1][1] + table[2][2]) % mod);
  result.insert((table[0][2] + table[1][1] + table[2][0]) % mod);
  return (result.size() == 1);
}

int dfs(int pos,int used,int mod){
  if(pos >= 9 && check(mod)){
    return 1;
  }
  else if(pos >= 9){
    return 0;
  }
  int res = 0;
  int x = pos % 3;
  int y = pos / 3;

  if(table[y][x] == 0){
    for(int i = 1; i <= 10; i++){
      if(used & (1<<i)) continue;
      table[y][x] = i;
      res += dfs(pos + 1, used | (1<<i),mod);
      table[y][x] = 0;
    }
  }
  else{
    res += dfs(pos + 1, used,mod);
  }
  return res;
}

int main(){
  while(~scanf("%d",&table[0][0])){
    int used = 0;
    if(table[0][0] != 0){
      used |= (1<<table[0][0]);
    }

    for(int y = 0; y < 3; y++){
      for(int x = 0; x < 3; x++){
        if(y == 0 && x == 0) continue;
        scanf("%d",&table[y][x]);
        if(table[y][x] != 0){
          used |= (1<<table[y][x]);
        }
      }
    }
    int mod;
    scanf("%d",&mod);
    if(mod == -1) break;

    used |= (1<<mod);
    int res = 0;
    if(mod == 0){
      int prev = used;
      for(int i = 1; i <= 10; i++){
        if(used & (1<<i)) continue;
        used |= (1<<i);
        mod = i;
        res += dfs(0,used,mod);
        used = prev;
      }
    }
    else{
      res += dfs(0,used,mod);
    }
    printf("%d\n",res);
  }
}
accuracy: 1
time_ms: 730
memory_kb: 1184
final_rank: 14

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <complex>
#include <assert.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <double,double> P;
 
static const int tx[] = {+0,+1,+0,-1};
static const int ty[] = {-1,+0,+1,+0};
 
static const double EPS = 1e-8;

int table[3][3];

bool check(int mod){
  set<int> result;
  for(int y = 0; y < 3; y++){
    int sum = 0;
    for(int x = 0; x < 3; x++){
      sum += table[y][x];
    }
    result.insert(sum % mod);
  }
  for(int x = 0; x < 3; x++){
    int sum = 0;
    for(int y = 0; y < 3; y++){
      sum += table[y][x];
    }
    result.insert(sum % mod);
  }
  result.insert((table[0][0] + table[1][1] + table[2][2]) % mod);
  result.insert((table[0][2] + table[1][1] + table[2][0]) % mod);
  return (result.size() == 1);
}

int dfs(int pos,int used,int mod){
  if(pos >= 9 && check(mod)){
    return 1;
  }
  else if(pos >= 9){
    return 0;
  }
  int res = 0;
  int x = pos % 3;
  int y = pos / 3;

  if(table[y][x] == 0){
    for(int i = 1; i <= 10; i++){
      if(used & (1<<i)) continue;
      table[y][x] = i;
      res += dfs(pos + 1, used | (1<<i),mod);
      table[y][x] = 0;
    }
  }
  else{
    res += dfs(pos + 1, used,mod);
  }
  return res;
}

int main(){
  while(~scanf("%d",&table[0][0])){
    int used = 0;
    if(table[0][0] != 0){
      used |= (1<<table[0][0]);
    }

    for(int y = 0; y < 3; y++){
      for(int x = 0; x < 3; x++){
        if(y == 0 && x == 0) continue;
        scanf("%d",&table[y][x]);
        if(table[y][x] != 0){
          used |= (1<<table[y][x]);
        }
      }
    }
    int mod;
    scanf("%d",&mod);
    if(mod == -1) break;

    used |= (1<<mod);
    int res = 0;
    if(mod == 0){
      int prev = used;
      for(int i = 1; i <= 10; i++){
        if(used & (1<<i)) continue;
        used |= (1<<i);
        mod = i;
        res += dfs(0,used,mod);
        used = prev;
      }
    }
    else{
      res += dfs(0,used,mod);
    }
    printf("%d\n",res);
  }
}
accuracy: 1
time_ms: 1310
memory_kb: 1188
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;



int p[11] = {};
int can[11] = {};
int m;

int check(int t){
	if( t < 4 ) return true;
	//if( t == 4 ) return (p[1]+p[4]+p[7]) % p[9] == 0;
	int c = (p[1]+p[4]+p[7]) % p[9];
	if( t == 5 ) return (p[0]+p[4]+p[8]) % p[9] == c;
	if( t == 6 ) return (p[0]+p[1]+p[2]) % p[9] == c;
	if( t == 7 ) return (p[2]+p[5]+p[8]) % p[9] == c;
	if( t == 8 ) return (p[6]+p[7]+p[8]) % p[9] == c && (p[2]+p[4]+p[6]) % p[9] == c;
	if( t == 9 ) return (p[0]+p[3]+p[6]) % p[9] == c && (p[3]+p[4]+p[5]) % p[9] == c;
	return true;
}
//

int d[] = {9,0,1,4,7,8,2,5,6,3};

int solve(int t){
	/*cout << t << endl;
	cout << "[" << p[9] << "]" << endl;
	for(int i = 0 ; i < 3 ; i++){
		for(int j = 0 ; j < 3 ; j++) cout << p[i*3+j] << " ";
		cout << endl;
	}
	*/
	if( !check(t-1) ) return 0;
	if( t == 10 ){
		return 1;
	}
	int x = d[t];
	if( p[x] == 0 ){
		int ans = 0;
		for(int i = 1 ; i <= 10 ; i++){
			if( can[i] ){
				can[i] = false;
				p[x] = i;
				ans += solve(t+1);
				p[x] = 0;
				can[i] = true;
			}
		}
		return ans;
	}else{
		return solve(t+1);
		
	}
}

int main(){
	while(1){
		for(int i = 0 ; i < 11 ; i++) can[i] = true;
		
		for(int i = 0 ; i < 10 ; i++){
			cin >> p[i];
			can[p[i]] = false;
		}
		if(p[0]==-1)break;
		cout << solve(0) << endl;
	}
}
accuracy: 1
time_ms: 50
memory_kb: 1160
final_rank: 2

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int a[11];
int used[11];
int sv=0;
int solve(int v){
	if(v==9){
	//	for(int i=1;i<11;i++)if(!used[i])return 0;
		return 1;
	}
	int ret=0;
	if(a[v]){
		bool ok=true;
		int i=a[v];
		if(v==2)sv=(a[0]+a[1]+a[2])%a[9];
		if(v%3==2){
			if((a[v-2]+a[v-1]+i)%a[9]!=sv)ok=false;
		}
		if(v>=6){
			if((a[v-6]+a[v-3]+i)%a[9]!=sv)ok=false;
		}
		if(v==6){
			if((a[2]+a[4]+i)%a[9]!=sv)ok=false;
		}
		if(v==8){
			if((a[0]+a[4]+i)%a[9]!=sv)ok=false;
		}
		if(ok){
			return solve(v+1);
		}else return 0;
	}
	for(int i=1;i<=10;i++){
		if(used[i])continue;
		bool ok=true;
		if(v==2)sv=(a[0]+a[1]+i)%a[9];
		if(v%3==2){
			if((a[v-2]+a[v-1]+i)%a[9]!=sv)ok=false;
		}
		if(v>=6){
			if((a[v-6]+a[v-3]+i)%a[9]!=sv)ok=false;
		}
		if(v==6){
			if((a[2]+a[4]+i)%a[9]!=sv)ok=false;
		}
		if(v==8){
			if((a[0]+a[4]+i)%a[9]!=sv)ok=false;
		}
		if(!ok)continue;
		used[i]=1;
		a[v]=i;
		ret+=solve(v+1);
		used[i]=0;
		a[v]=0;
	}
	//printf("%d: %d\n",v,ret);
	return ret;
}
int main(){
	while(1){
		for(int i=0;i<10;i++)scanf("%d",a+i);
		if(a[0]==-1)break;
		for(int i=0;i<11;i++)used[i]=0;
		for(int i=0;i<10;i++)if(a[i])used[a[i]]=1;
		if(a[9]){
			printf("%d\n",solve(0));
		}else{
			int ret=0;
			for(int i=1;i<=10;i++){
				if(used[i])continue;
				used[i]=1;
				a[9]=i;
				ret+=solve(0);
				used[i]=0;
			}
			printf("%d\n",ret);
		}
	}
}
accuracy: 1
time_ms: 60
memory_kb: 1028
final_rank: 1

code_snippet: #include<iostream>

using namespace std;

int a[10];

bool check(){
  static int c[][3]={
    {0,1,2},
    {3,4,5},
    {6,7,8},
    {0,3,6},
    {1,4,7},
    {2,5,8},
    {0,4,8},
    {2,4,6},
  };
  int r=-1;
  bool f=false;
  for(int j=0;j<8;j++){
    if(a[c[j][0]]==0||a[c[j][1]]==0||a[c[j][2]]==0)continue;
    int s=(a[c[j][0]]+a[c[j][1]]+a[c[j][2]])%a[9];
    if(r==-1){
      r=s;
    }else if(r!=s){
      f=true;
    }
  }
  return !f;
}

int dfs(int x,int u){
  if(x<0)return check();
  if(a[x])return dfs(x-1,u);
  int s=0;
  for(int i=1;i<=10;i++){
    if(u>>i&1)continue;
    a[x]=i;
    if(check()){
      s+=dfs(x-1,u|1<<i);
    }
    a[x]=0;
  }
  return s;
}

int main(){
  for(;;){
    int u=0;
    for(auto &e:a){
      cin>>e;
      u|=1<<e;
    }
    if(a[0]==-1)break;
    cout<<dfs(9,u)<<endl;
  }
}
accuracy: 1
time_ms: 120
memory_kb: 1160
final_rank: 3