4 Buttons
Bobo lives in an infinite chessboard. Initially he locates at
. There are
buttons.
When the first button is pressed, Bobo moves right for at most
cells.
When the second button is pressed, Bobo moves up for at most
cells.
When the third button is pressed, Bobo moves left for at most
cells.
When the fourth button is pressed, Bobo moves down for at most
cells.
Find the number of cells Bobo can reach modulo
, if he presses the buttons for no more than
times.
Input
The input consists of several test cases and is terminated by end-of-file.
Each test case contains five integers , , , and .
The number of test cases does not exceed
.
Output
For each test case, print an integer which denotes the result.
Note
For the first test case, Bobo can reach the following
cells:
,
,
,
,
,
,
,
,
,
,
.
思路:
画出图可以发现
会出现底层n - 1个方格,顶层1个方格,一共n-1层,一共四部分,面积分别为
,
加上一个 “十字架” ,长度为 t2 = 1 + (a + b + c + d) * n。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
int main()
{
ll n,a,b,c,d;
while(~scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&d))
{
ll t1 = ((n - 1) + 1) * (n - 1) / 2 % mod * (a * b % mod + b * c % mod + c * d % mod + d * a % mod) % mod;
ll t2 = (1 + (a + b + c + d) * n) % mod;;
printf("%lld\n",(t1 + t2) % mod);
}
return 0;
}