- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我参加了昨天举行的比赛。我无法解决下面的问题。比赛结束后我看到了一个人的解决方案,我无法理解。他是如何在这么短的时间内做到这一点的。他应用了什么。请解释他的解决方案。
You are given two numbers
N
andK
and a setX
.
X = { x : x is a natural number ≤ N }
You have to find the total number of pairs of elementsX[i]
andX[j]
belonging to the given set, such that,i < j
and their sum is divisible byK
.Input Format:
An integer
T
followed byT
lines, each containing a pair of space separated integersN
andK
.Output Format:
T
integers on separate lines. Each integer denotes the answer corresponding to that test case.Constraints:
1≤T≤100
K≤N≤10^9
1≤K≤10000
Sample Input(Plaintext Link)
2
10 4
7 3Sample Output(Plaintext Link)
10
7Explanation
For the 1st test case, there are 10 pairs whose sum is divisible by 4. (1,3), (1,7), (2,6), (2,10), (3,5), (3,9), (4,8), (5,7), (6,10) and (7,9)
For the 2nd test case, there are 7 pairs whose sum is divisible by 3. (1,2), (1,5), (2,4), (2,7), (3,6), (4,5) and (5,7)
解决方案--
#include<stdio.h>
int main()
{
long long int t,n,m,x,y,c=0;
scanf("%lld",&t);
while(t--)
{
scanf("%lld %lld",&n,&m);
c=0;
x=n/m;
y=n%m;
c+=((x*x*(m-1)-((m%2==0)?x:0))+x*(x-1))/2+y*x;
if(y>m/2)
c+=y-m/2;
printf("%lld\n",c);
}
}
最佳答案
提示
暴力方法:
您可以在 0<i<N
上编写双循环和i<j≤N
并计算这些对,使得 (i + j) % K == 0
.
更明智的方法:
对于给定的i
,数量j
这样(i + j) % K == 0
大约等于(N - i) / K
,如每个 K
第一个数字是 K
的倍数。 (您可以将近似值细化为精确表达式。)
更聪明的方法:
使用之前的结果,您需要对 (N - i)/K
求和为所有人贡献i
。此表达式对于 K
保持不变i
的连续值并从 i=1
开始减少(即 ~N/K
)到 i=N
(即 0
),步长为 1
。所以总计数将类似于 K.T(N/K)
,其中T(m)
表示m
第一个三角形数,m.(m+1)/2
.
(同样,您可以将近似值细化为精确值并找到封闭公式。)
示例:
N=9, K=3
1 => *2* 3 4 *5* 6 7 *8* 9 => 3
2 => 3 *4* 5 6 *7* 8 9 => 2
3 => 4 5 *6* 7 8 *9* => 2
4 => *5* 6 7 *8* 9 => 2
5 => 6 *7* 8 9 => 1
6 => 7 8 *9* => 1
7 => *8* 9 => 1
8 => 9 => 0
关于c - 无法理解竞争性考试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696011/
我正在准备 SQL Server 考试 (70-431)。我有 Sybex 的书 "SQL Server 2005 - Implementation and Maintenance" .我对估计一张
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the proper declaration of main? 我刚刚参加了第一次 C++
我刚刚参加了考试,被问到以下问题: Write the function body of each of the methods GenStrLen, InsertChar and StrRevers
如何通过 {exams} 包创建一个 moodle 问题,该问题至少为多项选择题打分? 我已经试过了: exams2moodle(..., mchoice = list(eval = list(neg
我正在为我们的项目进行 PAX 集成测试,我在类加载方面遇到了一些问题。 我在 PAX(使用的 karaf 容器)中部署了几个包。 karaf 启动后,我可以看到我的包和服务已启动并处于事件状态。但是
我正在尝试使用 PAX Exam 设置测试,如下所示: @ExamReactorStrategy(PerMethod.class) public class AbstractTest { @C
我正在参加过去的 Java 考试,但有一个问题一直困扰着我。 问题是:“任何构造函数显式或自动调用其父类的构造函数,父类调用其父类,依此类推类层次结构。这个过程的名称是什么? 感谢您的回答! 最佳答案
我正在准备 Java 程序员认证 (SCJP) 考试。关于异常的问题,当处理异常时,最好是处理特定的异常,如 NumberFormatException ,还是使用父级 Exception 类捕获所有
我一直在准备 SCJP,现在是 Oracle 认证专业 Java SE 程序员考试。 我很难理解所有不同的集合以及何时使用它们。我也喜欢闪存卡。因此,我尝试创建一组本质上相同的类,除了它们使用的集合之
在 MS Exam 70-536 .Net Foundation ,案例场景 1 中的第 3 章“搜索、修改和编码文本” Your organization, Northwind Traders, i
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
在MS Exam 70-536 .Net Foundation ,第1课Creating Threads中的Chapter 7“Threading”有一段文字: Be aware that becau
我正在与 exams2nops 合作R/exams 包的功能。由于我所在大学的学生注册号只有六个数字,我尝试调整了reglength exams2nops 中的选项(通常最少 7 个)功能以及nops
我的目标是使用 R/exams 和 Moodle 创建一个问题,包括在 Rmd 练习文件中生成的一些图。学生应口头描述情节,然后手动评分练习。 是否可以使用 exams2moodle为 Moodle
大家好,最近一次 MCQ 复习了我做错的答案。 函数 getSum 被定义为计算大小为 a[] 的 double 组中值的总和大小,大于给定的输入值阈值。下面显示了四个原型(prototype)定义,
我从官方 MCTS 考试 70-562 书中得到的小测试程序没有触发事件。我在 Debug模式下跟踪了计算机逻辑,它甚至没有进入我的事件,即使我已经设置好一切来处理它们。所以最大的问题是我做错了什么?
我已经开始使用 PAX-EXAM 和 Karaf 容器来测试我们的应用程序。有时,测试只是在测试方法开始之前挂起,并且始终卡在 cxf 注册 mbean 上: 正在注册 MBean org.apach
在第一版C# 70-483 Exam Ref , 示例 1-12 给出了将子任务附加到父任务的示例。我认为这是错误的,并希望有人在继续这个假设之前检查我的理解。示例中的代码如下: using Syst
我目前正在准备 Microsoft 考试 70-480。在实践测试中,我发现了以下问题,我无法解释其答案。 谁能解释为什么这是正确答案? You are developing a web page b
我正在阅读 MCTS 自定进度培训工具包(考试 70-536):Microsoft .NET Framework — 应用程序开发基础,第二版电子书。 现在我正在完成线程章节(第 7 期)。在第 2
我是一名优秀的程序员,十分优秀!