2019湖南省赛 4 Buttons(推公式)

4 Buttons
Bobo lives in an infinite chessboard. Initially he locates at ( 0 , 0 ) (0, 0) . There are 4 4 buttons.

When the first button is pressed, Bobo moves right for at most a a cells.
When the second button is pressed, Bobo moves up for at most b b cells.
When the third button is pressed, Bobo moves left for at most c c cells.
When the fourth button is pressed, Bobo moves down for at most d d cells.
Find the number of cells Bobo can reach modulo ( 1 0 9 + 7 ) (10^9+7) , if he presses the buttons for no more than n n times.

Input
The input consists of several test cases and is terminated by end-of-file.

Each test case contains five integers n n , a a , b b , c c and d d .

1 n , a , b , c , d 1 0 9 1 \leq n, a, b, c, d \leq 10^9
The number of test cases does not exceed 1 0 5 10^5 .
Output
For each test case, print an integer which denotes the result.

Note
For the first test case, Bobo can reach the following 11 11 cells: ( 3 , 0 ) (-3, 0) , ( 2 , 0 ) (-2, 0) , ( 1 , 0 ) (-1, 0) , ( 0 , 4 ) (0, -4) , ( 0 , 3 ) (0, -3) , ( 0 , 2 ) (0, -2) , ( 0 , 1 ) (0, -1) , ( 0 , 0 ) (0, 0) , ( 0 , 1 ) (0, 1) , ( 0 , 2 ) (0, 2) , ( 1 , 0 ) (1, 0) .

思路:
画出图可以发现
会出现底层n - 1个方格,顶层1个方格,一共n-1层,一共四部分,面积分别为 a b b c c d d a a*b 和 b*c 和 c*d 和 d*a
加上一个 “十字架” ,长度为 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;
}

发布了756 篇原创文章 · 获赞 27 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104642886