当前位置:网站首页>Luogu p3799 demon dream stick
Luogu p3799 demon dream stick
2022-07-01 08:17:00 【Study_ Study_ X】
Portal :https://www.luogu.com.cn/problem/P3799
The labels of the topics are combinatorics and violent enumeration . Take four sticks to form an equilateral triangle , Obviously, there are two equal , Form two sides , There are two more ( The two sticks may be equal or unequal ) Can form an edge . Then the problem turns into the given number ( That is, the length of the stick ) Two equal numbers found in a, Then find two more numbers b and c, bring a=b+c Calculate the number of all cases to get the answer .
If you save all the lengths in an array to run a double loop, it is bound to timeout , Because the biggest n Up to 100000 . So we must find a way to use a smaller array Save this large amount of data , Reduce the number of cycles in disguise . Personally, I think this is an important breakthrough of the problem , Because the length of the stick is less than fivethousand, but it is the largest n Is 100000 , Then there will be many duplicate numbers , So you can use a bucket of ideas , use num[ i ] The record length is i The number of sticks , This effectively reduces the number of cycles , It also facilitates calculation .
The next question is how to run the cycle , The outer cycle is i , List all lengths of the stick , As long as the number of pieces exceeds 2 You can enter the inner circulation , The inner circulation is j , Represents one of the constituent edges , The other length is i-j . Then use the knowledge of combinatorics to calculate the answer and take the module at any time .
Code up :
#include<iostream>
#define MAX ((int)1e9+7)
using namespace std;
int C(long long n, int m)
{
// Find the combination number
if (m == 1)return n;
if (m == 2)return (n * (n - 1) / 2) % MAX;
// Because in the title m Or 1 Or 2
// Two decisions can be directly used to deal with , Decision two remember to take the mold
}
int main(void)
{
int n = 0, number = 0, ans = 0;
int max = 0, num[5005] = { 0 };
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &number);
if (number > max) max = number;
// Record the longest stick length
num[number]++;
}
// Store all lengths of sticks
for (int i = 2; i <= max; i++)
{
if (num[i] >= 2) {
for (int j = 1; j <= i / 2; j++)
{
if (i - j == j && num[j] >= 2)
ans += (C(num[i], 2) * C(num[j], 2)) % MAX;
//i It happens to be 2*j when
else if (i - j != j && num[j] >= 1 && num[i - j] >= 1)
ans += ((C(num[i], 2) * C(num[j], 1)) % MAX * C(num[i - j], 1)) % MAX;
// Remember to withdraw the balance at any time
}
}
ans %= MAX;
}
printf("%d", ans);
return 0;// End of the flower
}
边栏推荐
- Access report realizes subtotal function
- getInputStream() has already been called for this request
- XX攻击——反射型 XSS 攻击劫持用户浏览器
- 5大组合拳,解决校园6大难题,护航教育信息化建设
- Li Kou daily question - Day 32 -1822 Symbol of array element product
- Deep learning systematic learning
- Php laraver Wechat payment
- web254
- Scala language learning-07-constructor
- Lm08 mesh series mesh inversion (fine)
猜你喜欢

使用 setoolkit 伪造站点窃取用户信息

Learn the knowledge you need to know about the communication protocol I2C bus

When using charts to display data, the time field in the database is repeated. How to display the value at this time?

5大组合拳,解决校园6大难题,护航教育信息化建设

CPU设计实战-第四章实践任务一简单CPU参考设计调试

OJ input and output exercise

【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景

Adding color blocks to Seaborn clustermap matrix
![[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion](/img/ce/6c9e4f2c54710610e8b1f68d6d8088.png)
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion

软键盘高度报错
随机推荐
【入门】取近似值
Scala语言学习-07-构造器
Significance and measures of source code encryption
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
Latex table
[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
Book of quantitative trading - reading notes of the man who conquers the market
Aardio - Shadow Gradient Text
CPU设计实战-第四章实践任务一简单CPU参考设计调试
[force deduction 10 days SQL introduction] Day9 control flow
事务方法调用@Transactional
PHP laravel wechat payment
Transaction method call @transactional
Microsoft stream - how to modify video subtitles
[getting started] input n integers and output the smallest K of them
Codeforces Round #803 (Div. 2) VP补题
Set up file server Minio for quick use
SQL number injection and character injection
How to check ad user information?