当前位置:网站首页>P3166 number triangle (tolerance and exclusion +gcd)
P3166 number triangle (tolerance and exclusion +gcd)
2022-07-26 01:47:00 【to cling】
Problem
Given a N × M N\times M N×M The grid of , Please calculate the total number of triangles with three points on the grid . Note that the three points of a triangle cannot be collinear .
Solution
- For two points in the grid , Let the difference between the horizontal and vertical coordinates be :i,j
Then the number of integer lattice points on the line between these two points is : g c d ( i , j ) − 1 gcd(i, j) - 1 gcd(i,j)−1
prove : At two o 'clock (0,0) and (6, 9) The difference between the horizontal and vertical coordinates of is (6, 9).
The point of (2, 3),(4, 6),(6, 9) These three points are on the line between these two points .
( According to this conclusion : If the difference between the coordinates of two points (i,j) The greatest common divisor of is 1, Then there is no integer lattice on its line )
Code
int gcd(int a, int b)
{
if (!b) return a;
return gcd(b, a % b);
}
ll C(ll x)
{
return x * (x - 1) * (x - 2) / 6;
}
int main()
{
IOS;
ll n, m;
cin >> n >> m;
n++; m++;
ll ans = C(n * m);
ans -= n * C(m) + m * C(n);
for (int i = 1; i < n; i++)
for (int j = 1; j < m; j++)
ans -= (n - i) * (m - j) * (gcd(i, j) - 1) * 2;
cout << ans << endl;
return 0;
}
边栏推荐
- npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
- “蔚来杯“2022牛客暑期多校训练营2 个人题解集合
- The sales volume has won the championship repeatedly. Is the secret of Wuling's success only low price?
- 【深入浅出玩转FPGA学习11----Testbench书写技巧1】
- Google gson usage details
- U++ learning notes ustruct, uenum declaration and function library simple function implementation
- zeromq浅析
- Record a failure caused by a custom redis distributed lock
- Ideal Path(UVA - 1599)
- 怎么使用宝塔面板把node全栈项目部署到服务器上
猜你喜欢

pdf. JS introduction

元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))

How to display numbers / English time in Excel

The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement

IDEA如何快速删除最近打开的项目
![Niuke - bm39 serialized binary tree [hard]](/img/c4/f14fe8488bbf28689fa3f02cdf4dae.png)
Niuke - bm39 serialized binary tree [hard]

销量连连夺冠,五菱的成功秘诀只有低价吗?

Digital transformation behind the reshaping growth of catering chain stores

Prime Ring Problem

8、学习MySQL 创建数据表
随机推荐
元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))
学习笔记:原码, 反码, 补码
IP address of the network
poj1521
C language enumeration types and unions
NFT access tool premint was hacked and lost more than 370000 US dollars
Oracle is nested at multiple levels, and the alias problem of the table cannot be found
餐饮连锁门店重塑增长背后的数字化转型
ABC find 4-cycle (pigeon nest theorem)
图像批处理高斯滤波降噪+峰值信噪比计算
Is it safe for Huatai Securities to open an account online? How to handle it?
Prime Ring Problem
Dataframe modifies the value of a row or column position
Leetcode 537. complex multiplication (netizens' thoughts, ashamed)
Is it safe to buy funds in stock accounts? Professional answers
U++ learning notes ustruct, uenum declaration and function library simple function implementation
3059. 雕塑(jzoj)
proto转换Dart | 项目使用Protobuf | flutter 使用grpc
Leetcode537. Complex multiplication (yes, solved)
What is a test case? How to design?