Skip to content

Commit 594749e

Browse files
authored
update: 添加问题“2894.分类求和并作差”的代码和题解(#959)
* 2894: WA.cpp (#958) Signed-off-by: LetMeFly666 <814114971@qq.com> * 2894: AC.(cpp+py) + WA.java (#958) cpp - AC,100.00%,19.79% py - AC,100.00%,88.02% Signed-off-by: LetMeFly666 <814114971@qq.com> * 2894: AC.(java+go) (#958) java - AC,100.00%,80.77% go - AC,100.00%,68.18% Signed-off-by: LetMeFly666 <814114971@qq.com> * update: 添加问题“2894.分类求和并作差”的代码和题解(#959) Signed-off-by: LetMeFly666 <814114971@qq.com> * typo: nums2 -> num2 Signed-off-by: LetMeFly666 <814114971@qq.com> --------- Signed-off-by: LetMeFly666 <814114971@qq.com>
1 parent ec516d5 commit 594749e

8 files changed

+192
-103
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-27 21:42:21
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-27 21:53:29
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int differenceOfSums(int n, int m) {
14+
return (1 + n) * n / 2 - (m + n / m * m) * (n / m);
15+
}
16+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-27 21:42:21
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-27 21:58:33
6+
*/
7+
package main
8+
9+
func differenceOfSums(n int, m int) int {
10+
return (1 + n) * n / 2 - (m + n / m * m) * (n / m)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-27 21:42:21
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-27 21:57:48
6+
*/
7+
class Solution {
8+
public int differenceOfSums(int n, int m) {
9+
return (1 + n) * n / 2 - (m + n / m * m) * (n / m);
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-27 21:42:21
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-27 21:54:46
6+
'''
7+
class Solution:
8+
def differenceOfSums(self, n: int, m: int) -> int:
9+
return (1 + n) * n // 2 - (m + n // m * m) * (n // m)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@
869869
|2859.计算K置位下标对应元素的和|简单|<a href="https://leetcode.cn/problems/sum-of-values-at-indices-with-k-set-bits/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/25/LeetCode%202859.%E8%AE%A1%E7%AE%97K%E7%BD%AE%E4%BD%8D%E4%B8%8B%E6%A0%87%E5%AF%B9%E5%BA%94%E5%85%83%E7%B4%A0%E7%9A%84%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135836080" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/sum-of-values-at-indices-with-k-set-bits/solutions/2618937/letmefly-2859ji-suan-k-zhi-wei-xia-biao-usyx1/" target="_blank">LeetCode题解</a>|
870870
|2860.让所有学生保持开心的分组方法数|中等|<a href="https://leetcode.cn/problems/happy-students/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/09/04/LeetCode%202860.%E8%AE%A9%E6%89%80%E6%9C%89%E5%AD%A6%E7%94%9F%E4%BF%9D%E6%8C%81%E5%BC%80%E5%BF%83%E7%9A%84%E5%88%86%E7%BB%84%E6%96%B9%E6%B3%95%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141905408" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/happy-students/solutions/2905023/letmefly-2860rang-suo-you-xue-sheng-bao-t8u1o/" target="_blank">LeetCode题解</a>|
871871
|2864.最大二进制奇数|简单|<a href="https://leetcode.cn/problems/maximum-odd-binary-number/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/13/LeetCode%202864.%E6%9C%80%E5%A4%A7%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%A5%87%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136669293" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-odd-binary-number/solutions/2683729/letmefly-2864zui-da-er-jin-zhi-qi-shu-ta-30tq/" target="_blank">LeetCode题解</a>|
872+
|2894.分类求和并作差|简单|<a href="https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/27/LeetCode%202894.%E5%88%86%E7%B1%BB%E6%B1%82%E5%92%8C%E5%B9%B6%E4%BD%9C%E5%B7%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148266123" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference/solutions/3687620/letmefly-2894fen-lei-qiu-he-bing-zuo-cha-suyc/" target="_blank">LeetCode题解</a>|
872873
|2900.最长相邻不相等子序列I|简单|<a href="https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/15/LeetCode%202900.%E6%9C%80%E9%95%BF%E7%9B%B8%E9%82%BB%E4%B8%8D%E7%9B%B8%E7%AD%89%E5%AD%90%E5%BA%8F%E5%88%97I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147990003" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-unequal-adjacent-groups-subsequence-i/solutions/3677963/letmefly-2900zui-chang-xiang-lin-bu-xian-69ua/" target="_blank">LeetCode题解</a>|
873874
|2903.找出满足差值条件的下标I|简单|<a href="https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/25/LeetCode%202903.%E6%89%BE%E5%87%BA%E6%BB%A1%E8%B6%B3%E5%B7%AE%E5%80%BC%E6%9D%A1%E4%BB%B6%E7%9A%84%E4%B8%8B%E6%A0%87I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139195914" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-indices-with-index-and-value-difference-i/solutions/2789520/letmefly-2903zhao-chu-man-zu-chai-zhi-ti-m92q/" target="_blank">LeetCode题解</a>|
874875
|2908.元素和最小的山形三元组I|简单|<a href="https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/29/LeetCode%202908.%E5%85%83%E7%B4%A0%E5%92%8C%E6%9C%80%E5%B0%8F%E7%9A%84%E5%B1%B1%E5%BD%A2%E4%B8%89%E5%85%83%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137151595" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-sum-of-mountain-triplets-i/solutions/2714346/letmefly-2908yuan-su-he-zui-xiao-de-shan-h3s9/" target="_blank">LeetCode题解</a>|
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
title: 2894.分类求和并作差:数学O(1)一行解决
3+
date: 2025-05-27 22:01:09
4+
tags: [题解, LeetCode, 简单, 数学]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2894.分类求和并作差:数学O(1)一行解决
9+
10+
力扣题目链接:[https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference/](https://leetcode.cn/problems/divisible-and-non-divisible-sums-difference/)
11+
12+
<p>给你两个正整数 <code>n</code> 和 <code>m</code> 。</p>
13+
14+
<p>现定义两个整数 <code>num1</code> 和 <code>num2</code> ,如下所示:</p>
15+
16+
<ul>
17+
<li><code>num1</code>:范围 <code>[1, n]</code> 内所有 <strong>无法被 </strong><code>m</code><strong> 整除</strong> 的整数之和。</li>
18+
<li><code>num2</code>:范围 <code>[1, n]</code> 内所有 <strong>能够被 </strong><code>m</code><strong> 整除</strong> 的整数之和。</li>
19+
</ul>
20+
21+
<p>返回整数 <code>num1 - num2</code> 。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>n = 10, m = 3
29+
<strong>输出:</strong>19
30+
<strong>解释:</strong>在这个示例中:
31+
- 范围 [1, 10] 内无法被 3 整除的整数为 [1,2,4,5,7,8,10] ,num1 = 这些整数之和 = 37 。
32+
- 范围 [1, 10] 内能够被 3 整除的整数为 [3,6,9] ,num2 = 这些整数之和 = 18 。
33+
返回 37 - 18 = 19 作为答案。
34+
</pre>
35+
36+
<p><strong class="example">示例 2:</strong></p>
37+
38+
<pre>
39+
<strong>输入:</strong>n = 5, m = 6
40+
<strong>输出:</strong>15
41+
<strong>解释:</strong>在这个示例中:
42+
- 范围 [1, 5] 内无法被 6 整除的整数为 [1,2,3,4,5] ,num1 = 这些整数之和 = 15 。
43+
- 范围 [1, 5] 内能够被 6 整除的整数为 [] ,num2 = 这些整数之和 = 0 。
44+
返回 15 - 0 = 15 作为答案。
45+
</pre>
46+
47+
<p><strong class="example">示例 3:</strong></p>
48+
49+
<pre>
50+
<strong>输入:</strong>n = 5, m = 1
51+
<strong>输出:</strong>-15
52+
<strong>解释:</strong>在这个示例中:
53+
- 范围 [1, 5] 内无法被 1 整除的整数为 [] ,num1 = 这些整数之和 = 0 。
54+
- 范围 [1, 5] 内能够被 1 整除的整数为 [1,2,3,4,5] ,num2 = 这些整数之和 = 15 。
55+
返回 0 - 15 = -15 作为答案。
56+
</pre>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><strong>提示:</strong></p>
61+
62+
<ul>
63+
<li><code>1 &lt;= n, m &lt;= 1000</code></li>
64+
</ul>
65+
66+
67+
68+
## 解题方法:数学
69+
70+
这就需要拿出我们小学二年级就学过的**等差数列求和公式** $\lfloor\frac{(首项+尾项)\times 项数}{2}\rfloor$
71+
72+
另外有$num1-num2=(num1+num2)-num2$,而$num1+num2$就是整个数列$1$到$n$的和,$num2$就是首项为$m$尾项为$\lfloor\frac{n}m\rfloor\times m$项数为$\lfloor\frac{n}m\rfloor$的等差数列。
73+
74+
+ 时间复杂度$O(1)$
75+
+ 空间复杂度$O(1)$
76+
77+
### AC代码
78+
79+
#### C++
80+
81+
```cpp
82+
/*
83+
* @Author: LetMeFly
84+
* @Date: 2025-05-27 21:42:21
85+
* @LastEditors: LetMeFly.xyz
86+
* @LastEditTime: 2025-05-27 21:53:29
87+
*/
88+
class Solution {
89+
public:
90+
int differenceOfSums(int n, int m) {
91+
return (1 + n) * n / 2 - (m + n / m * m) * (n / m);
92+
}
93+
};
94+
```
95+
96+
#### Python
97+
98+
```python
99+
'''
100+
Author: LetMeFly
101+
Date: 2025-05-27 21:42:21
102+
LastEditors: LetMeFly.xyz
103+
LastEditTime: 2025-05-27 21:54:46
104+
'''
105+
class Solution:
106+
def differenceOfSums(self, n: int, m: int) -> int:
107+
return (1 + n) * n // 2 - (m + n // m * m) * (n // m)
108+
```
109+
110+
#### Java
111+
112+
```java
113+
/*
114+
* @Author: LetMeFly
115+
* @Date: 2025-05-27 21:42:21
116+
* @LastEditors: LetMeFly.xyz
117+
* @LastEditTime: 2025-05-27 21:57:48
118+
*/
119+
class Solution {
120+
public int differenceOfSums(int n, int m) {
121+
return (1 + n) * n / 2 - (m + n / m * m) * (n / m);
122+
}
123+
}
124+
```
125+
126+
#### Go
127+
128+
```go
129+
/*
130+
* @Author: LetMeFly
131+
* @Date: 2025-05-27 21:42:21
132+
* @LastEditors: LetMeFly.xyz
133+
* @LastEditTime: 2025-05-27 21:58:33
134+
*/
135+
package main
136+
137+
func differenceOfSums(n int, m int) int {
138+
return (1 + n) * n / 2 - (m + n / m * m) * (n / m)
139+
}
140+
```
141+
142+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/148266123)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/05/27/LeetCode%202894.%E5%88%86%E7%B1%BB%E6%B1%82%E5%92%8C%E5%B9%B6%E4%BD%9C%E5%B7%AE/)哦~
143+
>
144+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

WhatCanISay.md

Lines changed: 0 additions & 103 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)