题目相关

题目链接

AtCoder Regular Contest 107 B 题,https://atcoder.jp/contests/arc107/tasks/arc107_b。

Problem Statement

Given are integers N and K. How many quadruples of integers (a,b,c,d) satisfy both of the following conditions?

  • 1 ≤ a,b,c,d ≤ N
  • a+b−c−d=K

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.

Samples1

Sample Input 1

2 1

Sample Output 1

4

Explaination

Four quadruples below satisfy the conditions:

  • (a,b,c,d)=(2,1,1,1)
  • (a,b,c,d)=(1,2,1,1)
  • (a,b,c,d)=(2,2,2,1)
  • (a,b,c,d)=(2,2,1,2)

Samples2

Sample Input 2

2525 -425

Sample Output 2

10314607400

Constraints

  • 1≤N≤10^5
  • −2(N−1)≤K≤2(N−1)
  • All numbers in input are integers.

题解报告

题目翻译

给定两个整数 N 和 K,在 1 ~ N 中,有几个四元组 (a, b, c, d) 满足 a+b-c-d=K。

题目分析

首先不要被四元组给迷惑,题目只是要求求出数量,而不是找出所有的四元组。因此本题核心还是一个纯数学问题。

任何一个题目,我们都可以先考虑暴力枚举。由于数据的最大值为 10^5,使用暴力枚举四元组 (a, b, c, d) ,对应的时间复杂度就是 。即使简单优化,变成暴力枚举三元组,时间复杂度也是 。意味着最大可能需要计算 ,这肯定是一个 TLE 算法。

简化成为二元组

题目给出一个四元组计算,我们考虑一个简化版本,也就是两元组计算。即满足 a+b=K。或者量化看,a=9、b=7 和 k=13。我们可以枚举 a,通过 b=k-a 来计算 b。也就是说,我们只需要枚举以下可能两元组:

(1, k-1)
(2, k-2)
.
.
.
(k-2, 2)
(k-1, 1)

统计这些两元组中有几个可能存在就可以得到答案。这样就是一个 O(N) 的算法。

为什么是从 1 开始枚举呢?题目告诉我们

四元组问题

通过上面的描述,我们可以将四元组转换为二元组,也就是将 a+b 看成一个数,c+d 看成另外一个数,这样 a+b-c-d=k 的问题,就可以转化成为一个二元组问题。

根据题目给出的数据,我们知道 1 ≤ a,b,c,d ≤ N,也就意味着 2 ≤ a+b, c+d ≤ 2*N。这样问题就化简为一个 O(N) 级别。

数据分析

由于在计算中,需要使用到乘法原理,最大数据可能是 ,必须使用 long long 这个数据类型表示。

AC 参考代码

//https://atcoder.jp/contests/arc107/tasks/arc107_b
//B - Quadruple #include <bits/stdc++.h>using namespace std;typedef long long LL;LL n,k;LL solve(LL sum) {LL tot = sum-1;LL sub = max(0LL, sum-n-1);return max(0LL, tot-2*sub);
}int main() {cin>>n>>k;LL ans=0;for (LL i=k; i<=2*n; i++) {ans += (solve(i)*solve(i-k));}cout<<ans<<"\n";return 0;
}

代码细节

可能有人要问 max() 这个函数。

首先、max() 出自 STL 的 algorithm 库。

其次、max() 只能比较相同数据类型,所以使用 0LL 这个小技巧。表示这是一个 long long 的 0。

AtCoder题解——AtCoder Regular Contest 107——B - Quadruple相关推荐

  1. AtCoder Regular Contest 107 B - Quadruple(数学+思维)

    题目链接:https://atcoder.jp/contests/arc107/tasks/arc107_b 给出两个数,n,k,现在有四个数,1<=a,b,c,d<=n ,要求满足等式 ...

  2. AtCoder Regular Contest 107 B - Quadruple

    思路: 可以设 x = a + b , y = c + d x=a+b,y=c+d x=a+b,y=c+d,那么问题就转换为了 x = k + y x=k+y x=k+y. 限制条件就是 2 ≤ x ...

  3. AtCoder Regular Contest 107 F - Sum of Abs(网络流最小割)

    题目链接 题意就是给定一个无向图,每个点有权值ai,bia_i,b_iai​,bi​,现在需要删去其中的一些点,其中删去一个点的花费为aia_iai​,删点后的图的分数为每一个联通块的分数之和,一个联 ...

  4. AtCoder题解 —— AtCoder Beginner Contest 182 —— D - Wandering

    题目相关 题目链接 AtCoder Beginner Contest 182 D 题,https://atcoder.jp/contests/abc182/tasks/abc182_d. Proble ...

  5. AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S

    题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...

  6. AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划

    题目相关 题目链接 AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b. Problem S ...

  7. AtCoder题解 —— AtCoder Beginner Contest 187 —— B - Gentle Pairs —— 暴力

    题目相关 题目链接 AtCoder Beginner Contest 187 B 题,https://atcoder.jp/contests/abc187/tasks/abc187_b. Proble ...

  8. AtCoder题解—— AtCoder Beginner Contest 181 —— B - Trapezoid Sum

    题目相关 题目链接 AtCoder Beginner Contest 181 B 题,https://atcoder.jp/contests/abc181/tasks/abc181_b. Proble ...

  9. Atcoder题解与视频集

    开启Atcoder之路 开启Atcoder之路_sortmin的博客-CSDN博客_atcoder怎么用 atcoder心得 atcoder心得_404REN的博客-CSDN博客_atcoder怎么用 ...

最新文章

  1. 某小公司:MySQL连环问
  2. 修改class文件_VM实战(六) - 通过案例深入学习class文件结构原理
  3. nginx常用代理配置
  4. shiro简单入门介绍
  5. 前端学习(1983)vue之电商管理系统电商系统之清空表格数据
  6. 创建react应用程序_如何使您的React应用程序具有完整的功能,完全的React性并能够处理所有这些疯狂的事情……...
  7. shell date cal
  8. 【渝粤教育】电大中专营销策划原理与实务 (2)_1作业 题库
  9. crawler4j源码学习(1):搜狐新闻网新闻标题采集爬虫
  10. 2022年道路运输企业安全生产管理人员考题及答案
  11. Linux上的Redis客户端软件G-dis3
  12. 废旧笔记本打造黑群晖NAS,docker,软路由,实现我心目中的all in one,包含fx n1,玩客云老母鸡玩法
  13. 20幻读是什么,幻读有什么问题
  14. 10月24日——程序猿的节日
  15. Spark小文件合并
  16. Tony.SerialPorts.RS232串口参数配置模块:扫描事件例程
  17. Android性能优化的问题
  18. 培养创造性思维的20个技巧!
  19. 【C++11】包装器
  20. 推荐几个美女和年薪百万大佬的公众号!

热门文章

  1. go 所有routine完成后才退出
  2. js的异步与宏任务(marcroTask)和微任务(microTask)
  3. linux mate 中文界面,Linux Mint 16 Cinnamon与Mate特性与界面
  4. 吃货联盟订餐系统(订餐功能实现)
  5. 题目:Minimize Ordering
  6. matlab最小二乘法拟合论文,最小二乘法原理,拟合(matlab)。
  7. firefox PAC代理
  8. 室内全彩led显示屏常见五种设计方案跟优劣势对比分析
  9. python reload is not defined_name 'reload' is not defined解决方法
  10. Mysql数据库备份与恢复测试