您的位置:首页 > 其它

CF711B(Codeforces Round #369 (Div. 2) - B)

2016-09-05 07:39 260 查看
Problem : Chris and Magic Square

Description :

给你一个矩阵,里面只有一个位置是0,代表这让你填入一个数,使得,每行的和与每列的和与两条对角线上的和都是一样的。

Solution :

模拟就好了,不过,中间计算过程会超过longlong的范围,因此要写个大整数类。这就是唯一的亮点了。

Code(C++) :

#include  <stdio.h>
#include <string.h>

#include <iostream>
#include <algorithm>

using namespace std;

const int M=500+5;
const int LEN=30;

struct BigInteger{
char a[LEN];
int len;
BigInteger(){}
BigInteger(char a[],int len)
4000
{
memset(this->a,0,sizeof(this->a));
strcpy(this->a,a);
this->len=len;
}

bool operator==(BigInteger b)
{
return !strcmp(a,b.a);
}

BigInteger operator+(BigInteger b)
{
char ans[LEN]={0};
char str1[LEN]={0};
char str2[LEN]={0};
strcpy(str1,a);
strcpy(str2,b.a);
for(int i=0;i<len/2;i++)
swap(str1[i],str1[len-i-1]);
for(int i=0;i<b.len/2;i++)
swap(str2[i],str2[b.len-i-1]);
int L;
for(L=0;L<len&&L<b.len;L++)
ans[L]=str1[L]-'0'+str2[L]-'0';
if(L==len)
for(int i=len;i<b.len;i++)
ans[i]=str2[i]-'0';
else
for(int i=b.len;i<len;i++)
ans[i]=str1[i]-'0';
int top=len>b.len? len:b.len;
for(int i=0;i<top;i++)
if(ans[i]>9)
ans[i]-=10,++ans[i+1];
if(ans[top])
++top;
for(int i=0;i<top;i++)
ans[i]+='0';
for(int i=0;i<top/2;i++)
swap(ans[i],ans[top-i-1]);
return BigInteger(ans,top);
}

BigInteger operator-(BigInteger b)
{
char ans[LEN]={0};
char str1[LEN]={0};
char str2[LEN]={0};
strcpy(str1,a);
strcpy(str2,b.a);
for(int i=0;i<len/2;i++)
swap(str1[i],str1[len-i-1]);
for(int i=0;i<b.len/2;i++)
swap(str2[i],str2[b.len-i-1]);
int L;
for(L=0;L<len&&L<b.len;L++)
ans[L]=str1[L]-'0'-(str2[L]-'0');
if(L==len)
for(int i=len;i<b.len;i++)
ans[i]=str2[i]-'0';
else
for(int i=b.len;i<len;i++)
ans[i]=str1[i]-'0';
int top=len>b.len? len:b.len;
for(int i=0;i<top;i++)
if(ans[i]<0)
ans[i]+=10,--ans[i+1];
//if(!ans[top-1])
//  --top;
for(;top>=1&&ans[top-1]==0;top--);
if(!top)
top=1;
for(int i=0;i<top;i++)
ans[i]+='0';
for(int i=0;i<top/2;i++)
swap(ans[i],ans[top-i-1]);
return BigInteger(ans,top);
}
};

BigInteger map[M][M];
int n;

int main()
{
//freopen("in.data","r",stdin);
while(~scanf("%d",&n)){
int x,y;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
char str[LEN];
scanf("%s",str);
map[i][j]=BigInteger(str,strlen(str));
if(map[i][j]==BigInteger("0",1)){
x=i;
y=j;
}
}
if(n==1){
if(map[0][0]==BigInteger("0",1))
puts("1");
else
puts(map[0][0].a);
continue;
}
int pos=-1;
for(int i=0;i<n;i++)
if(i!=x){
pos=i;
break;
}
BigInteger tag("0",1);
for(int j=0;j<n;j++)
tag=tag+map[pos][j];
BigInteger tmp("0",1);
for(int j=0;j<n;j++)
tmp=tmp+map[x][j];
map[x][y]=tag-tmp;
BigInteger t("0",1);
bool f=true;
if(map[x][y]==BigInteger("0",1)){
f=false;
goto F;
}
for(int i=0;i<n;i++){
BigInteger t("0",1);
for(int j=0;j<n;j++)
t=t+map[i][j];
if(!(t==tag)){
f=false;
goto F;
}
}
for(int j=0;j<n;j++){
BigInteger t("0",1);
for(int i=0;i<n;i++)
t=t+map[i][j];
if(!(t==tag)){
f=false;
goto F;
}
}
for(int k=0;k<n;k++)
t=t+map[k][k];
if(!(t==tag)){
f=false;
goto F;
}
t=BigInteger("0",1);
for(int k=0;k<n;k++)
t=t+map[k][n-k-1];
if(!(t==tag))
f=false;
F:;
if(f)
puts(map[x][y].a);
else
puts("-1");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: