# legend_noa's blog

LFYZ蒟蒻竟然写题解！！！……

## 【NOIp2004提高组】食虫算 题解

 43#9865#045
+  8468#6633
44445509678


 BADC
+CBDA
______
DCCC


## 输入输出样例

### 输入样例#1：

5
ABCED
BDACE
EBBAA


### 输出样例#1：

1 0 3 4 2


## 题解

$x +y = 10 x - 3y = 6$

$\begin{Bmatrix}1 & 1\\1 & -3\end{Bmatrix}\begin{Bmatrix}x & y\end{Bmatrix} =\begin{Bmatrix}10 \\ 6\end{Bmatrix}（1）$

$\begin{matrix}1 & 1 & 10 \\1 & -3 & 6\end{matrix}$

$\begin{Bmatrix}a_{11} & a_{12} & a_{13} & a_{14} & ... & a_{1n} \\ a_{21} & a_{22} & a_{23} & a_{24} & ... & a_{2n} \\ ...&...&...&...&...&... \\ a_{n1} &...& ... &...&...& a_{nn} \end{Bmatrix} （2）$

$\begin{Bmatrix}a_{11} & a_{12} & a_{13} & a_{14} & ... & a_{1n} \\ 0 & a_{22} & a_{23} & a_{24} & ... & a_{2n} \\ 0 & 0 & a_{33} & ... & ...&a_{3n} \\...&...&...&...&...&... \\ 0 & 0 & 0 & 0 & ...& a_{nn} \end{Bmatrix}\begin{Bmatrix}c_1 \\ c_2 \\ c_3 \\ ...\\ c_n\end{Bmatrix} = \begin{Bmatrix}b_1 \\ b_2 \\ b_3 \\ ... \\ b_n \end{Bmatrix} (3)$

1 -2 3 6
4 -5 6 12
7 -8 10 21


7 -8 10 21
4 -5 6 12
1 -2 3 6


1 -8/7 10/7 3
4 -5 6 12
1 -2 3 6


1 -8/7 10/7 3
0 -3/7 2/7 0
0 -6/7 11/7 3


#include<bits/stdc++.h>
const double EPS = 1e-8;
double b[111][111];
int n;
int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++){ //方便起见，我们从0开始编号
for(int j = 0; j < n; j++)
scanf("%lf", &b[i][j]);
scanf("%lf",&b[i][n]);
}
for(int i = 0; i < n; i++){
int pos = i;
for(int j = i; j < n; j++)
if(fabs(b[i][j]-b[pos][i]) <= EPS) pos = j; //调整矩阵
for(int j = 0; j <= n; j++) std::swap(b[i][j], b[pos][j]);
if(fabs(b[i][i]) <= EPS){ printf("No Solution\n"); return 0;}
for(int j = i+1; j <= n; j++) b[i][j] /= b[i][i];  // 化简方程
for(int j = 0; j < n; j++) if(i != j) for(int k = i+1; k <= n; k++) b[j][k] -= b[j][i]*b[i][k]; // 消元
}
for(int i = 0; i < n; i++) printf("%.2lf\n", b[i][n]);
return 0;
}



$\begin{bmatrix} -1 & 0 & 0 & 1 & 1 \\ -1 & 0 & 1 & 0 & 1 \\ 1 & -1 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ 1 & 1 & 0 & 0 & -1 \end {bmatrix} \begin{bmatrix} A\\B\\C\\D\\E \end {bmatrix} = \begin{bmatrix} {0\ \ \ \ \ or\ \ \ \ \ n }\\{0\ \ or\ \ -1\ \ or\ \ n-1}\\{0\ \ or\ \ -1\ \ or\ \ n-1}\\{0\ \ or\ \ -1\ \ or\ \ n-1}\\{0\ \ or\ \ -1\ \ or\ \ n-1} \end {bmatrix}$

#include<bits/stdc++.h>
#define MAXN 26
int n,equ,var,d[MAXN+10],x[MAXN+10];bool vis[MAXN+10];char s[3][MAXN+10];
typedef int matrix[MAXN+10][MAXN+10];matrix a,g;
int gcd(int a,int b) {	int t;	while(b) {t=a%b;a=b;b=t;}	return a;}
bool check() {
memset(vis,0,sizeof vis);
for(int i=1; i<=n; i++) {		x[i]=0;
for(int j=1; j<=n; j++)	x[i]+=g[i][j]*d[j];
if(x[i]%a[i][i]||x[i]/a[i][i]<0||x[i]/a[i][i]>=n||vis[x[i]/a[i][i]]) return 0;
x[i]/=a[i][i];		vis[x[i]]=1;
}
return 1;
}
void dfs(int i) {
if(i==n) {
if(check()) {
for(int i=1; i<n; i++)	printf("%d ",x[i]);
printf("%d\n",x[n]);   exit(0);
}return ;
}d[i]=1;	dfs(i+1);
d[i]=0;	dfs(i+1);
}
int main() {
scanf("%d", &n);	scanf("%s%s%s",s[0],s[1],s[2]);
for(int i=0; i<n; i++) {
for(int j=0; j<2; j++) a[n-i][s[j][i]-'A'+1]++;
a[n-i][s[2][i]-'A'+1]--;
}
for(int i=1; i<=n; i++)   g[i][i]=n,g[i][i-1]=-1;
g[1][0]=0;	equ=var=n;
for(int row=1, col=1; row<=equ&&col<=var; row++,  col++) {   int mxr=row; // 高斯消元，压行写法, 方便从字符中处理。
for(int i=row+1; i<=equ; i++)
if(abs(a[i][col])>abs(a[mxr][col])) mxr=i;
if(mxr!=row)	std::swap(a[row],a[mxr]), std::swap(g[row],g[mxr]);
if(!a[row][col]) {row--;continue;}
for(int i=1; i<=equ; i++)
if(i!=row&&a[i][col]) {
int lcm=a[i][col]/gcd(a[i][col],a[row][col])*a[row][col];
int t1=lcm/a[i][col],t2=lcm/a[row][col];
for(int j=1; j<=var; j++) {
g[i][j]=t1*g[i][j]-t2*g[row][j];
a[i][j]=t1*a[i][j]-t2*a[row][j];
}
}
}
dfs(1);
}



posted on 2018-10-23 20:43  legend_noa  阅读(152)  评论(0编辑  收藏  举报