CF711B(Codeforces Round #369 (Div. 2) - B)
2016-09-05 07:39
260 查看
Problem : Chris and Magic Square
Description :
给你一个矩阵,里面只有一个位置是0,代表这让你填入一个数,使得,每行的和与每列的和与两条对角线上的和都是一样的。
Solution :
模拟就好了,不过,中间计算过程会超过longlong的范围,因此要写个大整数类。这就是唯一的亮点了。
Code(C++) :
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; }
相关文章推荐
- 【MZ】CF 358D - 358E #209 (Div. 2)
- CF--#334-div2--B
- CF 179(div2) C(线段树 || 扫描法 )
- CF 173(div2) B
- 【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
- CF 165(div2)
- CF 277 div2 A. Calculating Function
- CF_#277.5_DIV2_A_B_C
- CF#277.5 (Div. 2) A.(技巧题,类似动规)
- CF 358D - 358E #209 (Div. 2)
- CF_Croc Champ 2012 - Round 2 (Unofficial Div. 2 Edition)
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
- 387 div2 cf 4
- CF-Codeforces Round #392 (Div. 2)-B-Blown Garland
- CF 280A. Vanya and Cubes(div2)
- Kolya and Tandem Repeat - CF#253 (Div. 2)B (443B) 哈希或水题
- CF Codeforces Round #256 (Div. 2) D (448D) Multiplication Table
- CF Round #324 (Div2) D. Dima and lisa
- CF#FF (Div. 2) C.
- 【MZ】CF 356A - 356D #207 (Div. 1)