比賽鏈接
AutoX安途杯中山大學程序設計校賽(同步賽)
C.Lucky Matrix
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
A \(matrix \begin{bmatrix} A & B & C \\ D & E & F \\ G & H & I \end{bmatrix}\) is a Lucky Matrix if and only if \(A, B, C, D, E, F, G, H, I\) are all integers and \(A + B + C = D + E + F = G + H + I = A + D + G = B + E + H = C + F + I = A + E + I = C + E + G\).
For example, these are Lucky Matrices:
\(\begin{bmatrix} 6 & 7 & 2 \\ 1 & 5 & 9 \\ 8 & 3 & 4 \end{bmatrix}\begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix} ~ \begin{bmatrix} -1 & 1 & 0 \\ 1 & 0 & -1 \\ 0 & -1 & 1 \end{bmatrix}\)
However, these are NOT Lucky Matrices: (Pay attention that in a Lucky Matrix all the 9 numbers must be integers.)
\(\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} ~ \begin{bmatrix} 1 & 0 & 0.5 \\ 0 & 0.5 & 1 \\ 0.5 & 1 & 0 \end{bmatrix} ~ \begin{bmatrix} 0 & 0 & 0.75 \\ 1 & 0.25 & -0.5 \\ -0.25 & 0.5 & 0.5 \end{bmatrix}\)
Now you are given the three integers \(A, B, D\) in the matrix, and you need to determine the values of \(C, E, F, G, H, I\) such that the matrix is a Lucky Matrix.
輸入描述:
The first line of input contains an integer \(T ~ (1 \leq T \leq 20000)\), denoting the number of test cases.
Each test case contains three integers \(A, B, D\) in one line. \(-10^6 \leq A,B,D \leq 10^6\).
輸出描述:
For each test case, print 6 integers \(C, E, F, G, H, I\) in one line, denoting your answer.
If there is no solution, print "-1". If there are multiple solutions, you can print any one of them.
輸入
5
6 7 1
1 1 1
-1 1 1
1 0 0
0 0 1
輸出
2 5 9 8 3 4
1 1 1 1 1 1
0 0 -1 0 -1 1
-1
-1
解題思路
數學
設 \(A+B+C=x\),則 \(G=x-A-D,C=x-A-B,H=x-B-E,F=x-D-E\),由 \(G+H+I=C+F+I=A+E+I\),即 \(G+H=x-A-D+x-B-E=C+F=x-A-B+x-D-E=A+E\),得 \(2E=2x-2A-B-D\),又有 \(C+E+G=E+x-A-B+x-A-D=x\),得 \(2E=4A-2B-2D-2x\),故有:\(2x-2A-B-D=4A-2B-2D-2x\),解得 \(x=\frac{6A+3B+3D}{4}\)
- 時間復雜度:\(O(1)\)
代碼
#include<cstdio>
using namespace std;
int main()
{
int t;
for(scanf("%d",&t);t;t--)
{
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
int k=6*a+3*b+3*d;
if(k%4)puts("-1");
else
{
k/=4;
int c=k-a-b,g=k-a-d,e=k-c-g,f=k-d-e,h=k-b-e,i=k-a-e;
printf("%d %d %d %d %d %d\n",c,e,f,g,h,i);
}
}
return 0;
}
D.Determinant
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
HZ is doing homework. The answer to a problem is a \(2 \times 2\) matrix. After trying for several hours, he finally solved it. But unfortunately, one of the four numbers in the matrix is replaced by \(x\) by accident. Besides the three remaining numbers, he only remembers that the determinant of the matrix is bigger than zero. Now your task is to compute the range of \(x\).
輸入描述:
The first line of input contains an integer \(T ~ (1 \leq T \leq 10^5)\), denoting the number of test cases.
Each test case contains four elements \(A, B, C, D\), denoting the matrix \(\begin{bmatrix} A & B \\ C & D \end{bmatrix}\). One of the elements is replaced by xx while the remaining three are integers.
\(-30 \leq A,B,C,D \leq 30\). (This is the range of the three given integers, not the range of \(x\). \(x\) can be any real number.)
輸出描述:
For each test case, print your answer in one line:
If the range of \(x\) is all real numbers, print "R";
If the range of \(x\) is empty set, print "E";
Otherwise, print "x>p/q" or "x<p/q" (\(q>0\), \(|p|\) and qq are coprime). Specially, if \(q=1\), you should only print "x>p" or "x<p".
輸入
11
x 2 3 4
1 x 3 4
1 2 x 4
1 2 3 x
x 2 3 -4
1 x -3 4
1 -2 x 4
-1 2 3 x
x 1 -1 0
1 x 0 -1
x 0 0 1
輸出
x>3/2
x<4/3
x<2
x>6
x<-3/2
x>-4/3
x>-2
x<-6
R
E
x>0
解題思路
二階行列式即正對角線的乘積減去副對角線的乘積,模擬一遍即可,關鍵在於找 \(x\) 的字符串的處理~
- 時間復雜度:\(O(1)\)
代碼
#include<bits/stdc++.h>
using namespace std;
string s;
int T;
string num[4];
int main()
{
cin>>T;
cin.ignore();
while(T--)
{
getline(cin,s);
int n=s.size();
int cnt=0;
int pos=s.find(' '),lst=0;
while(pos!=-1)
{
num[cnt++]=s.substr(lst,pos-lst);
lst=pos+1;
pos=s.find(' ',pos+1);
}
num[cnt++]=s.substr(lst,n-lst);
int a,b,c;
if(num[0]=="x"||num[3]=="x")
{
a=num[0]=="x"?stoi(num[3]):stoi(num[0]);
b=stoi(num[1]);
c=stoi(num[2]);
int t=b*c;
if(a==0)
puts(t<0?"R":"E");
else if(a>0)
{
int d=__gcd(a,abs(t));
t/=d,a/=d;
if(a==1)
printf("x>%d\n",t);
else
printf("x>%d/%d\n",t,a);
}
else
{
if(t<0)a=abs(a),t=abs(t);
else
t=-t,a=-a;
int d=__gcd(a,abs(t));
t/=d,a/=d;
if(a==1)
printf("x<%d\n",t);
else
printf("x<%d/%d\n",t,a);
}
}
else
{
a=num[1]=="x"?stoi(num[2]):stoi(num[1]);
b=stoi(num[0]);
c=stoi(num[3]);
int t=b*c;
if(a==0)
puts(t>0?"R":"E");
else if(a>0)
{
int d=__gcd(a,abs(t));
t/=d,a/=d;
if(a==1)
printf("x<%d\n",t);
else
printf("x<%d/%d\n",t,a);
}
else
{
if(t<0)a=abs(a),t=abs(t);
else
t=-t,a=-a;
int d=__gcd(a,abs(t));
t/=d,a/=d;
if(a==1)
printf("x>%d\n",t);
else
printf("x>%d/%d\n",t,a);
}
}
}
return 0;
}
E.Sequence I
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
You are given a sequence of length \(N\). Each time you can delete two adjacent numbers, and insert the absolute value of their difference in the corresponding position. What is the mininum number you can get after \(N-1\) operations? (The number you get means the only one integer left after \(N-1\) operations.)
輸入描述:
The first line of input contains an integer \(T ~ (1 \leq T \leq 1000)\), denoting the number of test cases.
Each test case contains two lines:
The first line contains one integer \(N ~ (2 \leq N \leq 8)\), denoting the length of the sequence;
The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N ~ (0 \leq A_i \leq 10^6)\), denoting the sequence.
輸出描述:
For each test case, print one integer in one line, denoting your answer.
輸入
5
2
1 1
2
2 3
2
3 2
3
999 1 1
4
80 87 50 54
輸出
0
1
1
997
3
解題思路
暴力dfs
數據量較小,可暴力dfs,即每次操作枚舉任意一個數進行操作,再輸出多出來的數與插入新數~
- 時間復雜度:\(O(n!)\)
代碼
#include<bits/stdc++.h>
using namespace std;
int t,n;
vector<int> a;
int res;
int op;
void dfs(int op)
{
if(op==0)
{
res=min(res,a[0]);
return ;
}
for(int i=0;i+1<a.size();i++)
{
auto tmp=a;
int t=abs(a[i]-a[i+1]);
a.erase(a.begin()+i,a.begin()+i+2);
a.insert(a.begin()+i,t);
dfs(op-1);
a=tmp;
}
}
int main()
{
for(scanf("%d",&t);t;t--)
{
scanf("%d",&n);
a.clear();
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
a.push_back(x);
}
res=0x3f3f3f3f;
op=n-1;
dfs(op);
printf("%d\n",res);
}
return 0;
}
