codeforces Gym 100418D BOPC 打表找规律,求逆元
BOPC
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86686#problem/D
Description
You invented a new chess figure and called it BOPC. Suppose it stands at the square grid at the point with coordinates X1, Y1. The point with coordinates X2, Y2 is under attack if |X1 - X2| < |Y1 - Y2|. Let the power of the figure denote the number of fields under attack for all possible starting positions of BOPC. Your goal is to calculate the power of BOPC figure given the field size.
Input
Single line containing one integer N — size of the field (1 ≤ N ≤ 109).
Output
Single line containing power of BOPC figure given the field size modulo 109 + 7.
Sample Input
3
Sample Output
26
HINT
题意
给你一个棋盘,棋盘上面有炮塔,只要满足|yj-yi|>|xj-xi|的地方,都能被攻击到
然后就问如果这个棋盘上面,全是炮塔的话,问你一共攻击了多少个地方(一个地方可以重复攻击的)
题解:
先打一个表,然后再找规律,不要问我为什么找到这个规律的,我也不造……
注意,除法求mod,要写一个逆元,除此之外,就应该没什么问题了
代码:
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const long long mod = 1e9 + 7; void extend_gcd(long long a , long long b , long long& d , long long & x , long long &y ) { if (!b) { d = a; x = 1; y = 0; } else { extend_gcd(b,a%b,d,y,x); y -= x*(a/b); } } long long inv(long long a,long long n) { long long d , x , y; extend_gcd(a,n,d,x,y); return d==1? (x+n) % n : -1; } int main(int argc,char *argv[]) { long long N; cin >> N; long long check = inv(6,mod); long long ANS = (((N*(N-1) % mod) * ((3*N*N - N + 2) % mod)) %mod * check) % mod; cout << ANS << endl; return 0; }