薛磊 Job Seeker

计蒜客 矩阵问题

2019-05-26
ACM

传送门

Problem Description

一个同学 LSQ 在小课堂后对矩阵产生极大的感兴趣,他想到了一个对矩阵求和的问题,但是这个矩阵实在太大了,他算不过来,你能帮帮他吗? 第一个坐标(a,c),第二个坐标(b,d)。 a<b<=10^18,c<d<=10^18。

Input

一行四个正整数a,b,c,d。

Output

一行一个整数表示答案

Sample Input

1 3 4 6

Sample Output

108

Solution

因为题目乘以2,可以想到两个数之间应该有关系。 由图看一看出第一行第i个数和最后一行倒数第i个数之和sum都是相等的。所以题答案等于,sum*矩阵大小/2*2 = sum*矩阵大小。 但是题目数值过大,所以需要进行求余。

#include <bits/stdc++.h>
using namespace std;
const long mo=332748118;
int main(void) {
	long height, width;
	long start, end;
	long a, b, c, d;
	cin >> a >> b >> c >> d;
	height = (b - a + 1) % mo;
	width = (d - c + 1) % mo;
	start = (a + c - 1);
	end = (b + d - 1);
	long result = (((height * width) % mo) * ((start + end) % mo)) % mo;
	cout << result << endl;
}

回到顶部


下一篇 AJAX

Comments

Content