思路:
势能分析法。
假设每颗星星的势能为
\(x^2+y^2\) 那么对于一行的两颗星星
\((i, j), (i, k), j < k\) 它转移到
\((i, j+1), (i, k-1)\)的势能变化为
\(j^2-(j+1)^2+k^2-(k-1)^2=2*(k-j-1)\) 正好是我们魔法值的两倍
代码:
#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#include using namespace std;#define y1 y11#define fi first#define se second#define pi acos(-1.0)#define LL long long#define ll long long//#define mp make_pair#define pb push_back#define ls rt<<1, l, m#define rs rt<<1|1, m+1, r#define ULL unsigned LL#define pll pair #define pli pair #define pii pair #define piii pair #define pdd pair #define mem(a, b) memset(a, b, sizeof(a))#define debug(x) cerr << #x << " = " << x << "\n";#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//headint n, m, a;LL x, y;int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { scanf("%d", &a); x += a*(i*i+j*j); } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { scanf("%d", &a); y += a*(i*i+j*j); } } printf("%lld\n", (x-y)/2); return 0;}