题目
During the quarantine, Sicromoft has more free time to create the new functions in “Celex-2021”. The developers made a new function GAZ-GIZ, which infinitely fills an infinite table to the right and down from the upper left corner as follows:
The cell with coordinates (x,y) is at the intersection of x-th row and y-th column. Upper left cell (1,1) contains an integer 1.
The developers of the SUM function don’t sleep either. Because of the boredom, they teamed up with the developers of the RAND function, so they added the ability to calculate the sum on an arbitrary path from one cell to another, moving down or right. Formally, from the cell (x,y) in one step you can move to the cell (x+1,y) or (x,y+1).
After another Dinwows update, Levian started to study “Celex-2021” (because he wants to be an accountant!). After filling in the table with the GAZ-GIZ function, he asked you to calculate the quantity of possible different amounts on the path from a given cell (x1,y1) to another given cell (x2,y2), if you can only move one cell down or right.
Formally, consider all the paths from the cell (x1,y1) to cell (x2,y2) such that each next cell in the path is located either to the down or to the right of the previous one. Calculate the number of different sums of elements for all such paths.
Input
The first line contains one integer t (1≤t≤57179) — the number of test cases.
Each of the following t lines contains four natural numbers x1, y1, x2, y2 (1≤x1≤x2≤109, 1≤y1≤y2≤109) — coordinates of the start and the end cells.
Output
For each test case, in a separate line, print the number of possible different sums on the way from the start cell to the end cell.
Example
Input
4
1 1 2 2
1 2 2 4
179 1 179 100000
5 7 5 7
Output
2
3
1
1
Note
In the first test case there are two possible sums: 1+2+5=8 and 1+3+5=9.
第一次:回溯超时
#include <iostream>
#include <cmath>
using namespace std;
struct point{
int x;
int y;
};
int ans(point begin, point end) {
if(begin.x == end.x || begin.y == end.y)
return 1;
else {
point a, b;
a.x = end.x - 1; a.y = end.y;
b.y = end.y - 1; b.x = end.x;
return ans(begin, a) + ans(begin, b);
}
}
int main(){
int t;
point be, en;
cin >> t;
while(t--) {
cin >> be.x >> be.y >> en.x >> en.y;
cout << ans(be, en) << endl;
}
return 0;
}
第二次:按照数学原理引入阶乘,超时
#include <iostream>
using namespace std;
int main() {
int t, x1, y1, x2, y2, m, n, fenzi, fenmu, i;
cin >> t;
while(t--) {
cin >> x1 >> y1 >> x2 >> y2;
m = x2 - x1;
n = y2 - y1;
if(m == 0 || n == 0)
cout << 1 << endl;
else {
fenzi = 1;
fenmu = 1;
for(i = 2; i <= (m + n); ++i)
fenzi *= i;
for(i = 2; i <= m; ++i)
fenmu *= i;
for(i = 2; i <= n; ++i)
fenmu *= i;
cout << fenzi / fenmu << endl;
}
}
return 0;
}
第三次:按照数学原理引入阶乘,用Python,语法错误
t = int(input())
for j in range(1,t):
str = input()
a = 0
before = 0
for k in range(1,len(str)):
if str[k] == ' ' and a = 0:
a = 1
x1 = int(str[0:k])
before = k+1
if str[k] == ' ' and a = 1:
a = 2
y1 = int(str[before:k])
before = k+1
if str[k] == ' ' and a = 2:
x2 = int(str[before:k])
before = k+1
if k == len(str)-1:
y2 = int(str[before:k+1])
m = x2 - x1;
n = y2 - y1;
if m == 0 or n == 0:
print(1)
else:
fenzi = 1
fenmu = 1
for i in range(2,(m + n)):
fenzi *= i
for i in range(2,m):
fenmu *= i
for i in range(2,n):
fenmu *= i
print(fenzi/fenmu)
正确版
#include <iostream>
using namespace std;
int main() {
long long t, x1, y1, x2, y2, m, n, fenzi, fenmu, i;
cin >> t;
while(t--) {
cin >> x1 >> y1 >> x2 >> y2;
cout << (x2 - x1) * (y2 - y1) + 1 << endl;
}
return 0;
}
所以这是什么骚操作???
大佬说:这就是找规律