A.根号2
链接:http://acm.fzu.edu.cn/problem.php?pid=2317
思路:根据题目中的公式写出a1,a2,a3分别与a0相减 观察得到任意ai与a0相减答案都是2*i,数据较大,开longlong
代码如下:
#include<iostream>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll K;
while(cin>>K){
cout<<2*K<<endl;
}
return 0;
}
B.矩阵 思路:先直接暴力求得主对角线元素和,在根据题目要求减去修改之前那一点的值然后加上修改之后的值。如果修改的是方阵a,则ans -= a[x][y]*b[y][x];a[x][y] = v;ans+=a[x][y]*b[y][x]‘如果修改的是方阵b同理。
代码如下:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
typedef long long ll;
ll a[1005][1005],b[1005][1005],c[1005][1005];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,q;
while(cin >> n >> q)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> b[i][j];
}
}ll ans = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
ans+=a[i][j]*b[j][i];
}
}
while(q--)
{
int k, x, y, v; cin >> k >> x >> y >>v;
if(!k)
{
ans -= a[x][y]*b[y][x];
a[x][y] = v;
ans+=a[x][y]*b[y][x];
}
else
{
ans -= b[x][y]*a[y][x];
b[x][y] = v;
ans+=b[x][y]*a[y][x];
}
cout << ans << endl;
}
}
return 0;
}

京公网安备 11010502036488号