Skip to content

Commit 61b6065

Browse files
authored
update: 添加问题“1922.统计好数字的数目”的代码和题解(#873)
* 1922: AC.(cpp+py) (#871) Signed-off-by: LetMeFly666 <[email protected]> * 1922: CE.java (#871) Signed-off-by: LetMeFly666 <[email protected]> * docs: en.白天积累 Signed-off-by: LetMeFly666 <[email protected]> * 1922: begin.go (#871) Signed-off-by: LetMeFly666 <[email protected]> * 3272: AC.go - AC,100.00%,63.16% (#867) Signed-off-by: LetMeFly666 <[email protected]> * 3272: AC.java - AC,100.00%,87.23% (#871) Signed-off-by: LetMeFly666 <[email protected]> * docs: readme.fix.2999 (#871) Signed-off-by: LetMeFly666 <[email protected]> * feat: 当存在对应题目issue时不再开启新issue close #872 Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“1922.统计好数字的数目”的代码和题解(#873) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 4e61e60 commit 61b6065

9 files changed

+322
-6
lines changed

Codes/1922-count-good-numbers.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-13 17:00:20
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-13 17:06:07
6+
* @Description: AC,100.00%,51.97%
7+
*/
8+
#if defined(_WIN32) || defined(__APPLE__)
9+
#include "_[1,2]toVector.h"
10+
#endif
11+
12+
typedef long long ll;
13+
const ll MOD = 1e9 + 7;
14+
15+
class Solution {
16+
private:
17+
ll pow(ll a, ll b) {
18+
ll ans = 1;
19+
while (b) {
20+
if (b & 1) {
21+
ans = ans * a % MOD;
22+
}
23+
a = a * a % MOD;
24+
b >>= 1;
25+
}
26+
return ans;
27+
}
28+
public:
29+
int countGoodNumbers(long long n) {
30+
return pow(5, (n + 1) / 2) * pow(4, n / 2) % MOD;
31+
}
32+
};

Codes/1922-count-good-numbers.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-13 17:54:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-14 00:05:17
6+
* @Description: continue on subway,太晃了还是先算了
7+
* @Description: AC,100.00%,63.16%
8+
*/
9+
package main
10+
11+
var MOD1922 = int64(1000000007)
12+
13+
func pow1922(a int64, b int64) int64 {
14+
ans := int64(1)
15+
for ; b > 0; b >>= 1 {
16+
if b & 1 == 1 {
17+
ans = ans * a % MOD1922
18+
}
19+
a = a * a % MOD1922
20+
}
21+
return ans
22+
}
23+
24+
func countGoodNumbers(n int64) int {
25+
return int(pow1922(5, (n + 1) / 2) * pow1922(4, n / 2) % MOD1922)
26+
}

Codes/1922-count-good-numbers.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-13 17:07:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-14 00:07:21
6+
* @Description: AC,100.00%,87.23%
7+
*/
8+
class Solution {
9+
private final long mod = 1000000007;
10+
11+
private long pow(long a, long b) {
12+
long ans = 1;
13+
while (b > 0) {
14+
if ((b & 1) == 1) {
15+
ans = ans * a % mod;
16+
}
17+
a = a * a % mod;
18+
b >>= 1;
19+
}
20+
return ans;
21+
}
22+
23+
public int countGoodNumbers(long n) {
24+
return (int)(pow(5, (n + 1) / 2) * pow(4, n / 2) % mod);
25+
}
26+
}

Codes/1922-count-good-numbers.py

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-04-13 17:06:16
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-13 17:06:17
6+
'''
7+
MOD = 1000000007
8+
9+
class Solution:
10+
def countGoodNumbers(self, n: int) -> int:
11+
return pow(5, (n + 1) // 2, MOD) * pow(4, n // 2, MOD) % MOD

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2022-05-19 18:48:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-04-11 22:38:06
5+
* @LastEditTime: 2025-04-14 00:11:36
66
-->
77
# LetLeet Blog
88

@@ -626,6 +626,7 @@
626626
|1884.鸡蛋掉落-两枚鸡蛋|中等|<a href="https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/13/LeetCode%201884.%E9%B8%A1%E8%9B%8B%E6%8E%89%E8%90%BD-%E4%B8%A4%E6%9E%9A%E9%B8%A1%E8%9B%8B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142906976" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/solutions/2949710/letmefly-1884ji-dan-diao-luo-liang-mei-j-saz6/" target="_blank">LeetCode题解</a>|
627627
|1901.寻找峰值II|中等|<a href="https://leetcode.cn/problems/find-a-peak-element-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/19/LeetCode%201901.%E5%AF%BB%E6%89%BE%E5%B3%B0%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135083347" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-a-peak-element-ii/solutions/2572012/letmefly-1901xun-zhao-feng-zhi-iier-fen-19tmj/" target="_blank">LeetCode题解</a>|
628628
|1911.最大子序列交替和|中等|<a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/11/LeetCode%201911.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%88%97%E4%BA%A4%E6%9B%BF%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131652316" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-alternating-subsequence-sum/solutions/2339029/letmefly-1911zui-da-zi-xu-lie-jiao-ti-he-fyzq/" target="_blank">LeetCode题解</a>|
629+
|1922.统计好数字的数目|中等|<a href="https://leetcode.cn/problems/count-good-numbers/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/14/LeetCode%201922.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E6%95%B0%E5%AD%97%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147200001" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-good-numbers/solutions/3650327/letmefly-1922tong-ji-hao-shu-zi-de-shu-m-ev7k/" target="_blank">LeetCode题解</a>|
629630
|1928.规定时间内到达终点的最小花费|困难|<a href="https://leetcode.cn/problems/minimum-cost-to-reach-destination-in-time/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/03/LeetCode%201928.%E8%A7%84%E5%AE%9A%E6%97%B6%E9%97%B4%E5%86%85%E5%88%B0%E8%BE%BE%E7%BB%88%E7%82%B9%E7%9A%84%E6%9C%80%E5%B0%8F%E8%8A%B1%E8%B4%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142691241" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-to-reach-destination-in-time/solutions/2937779/letmefly-1928gui-ding-shi-jian-nei-dao-d-h1rk/" target="_blank">LeetCode题解</a>|
630631
|1944.队列中可以看到的人数|困难|<a href="https://leetcode.cn/problems/number-of-visible-people-in-a-queue/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/05/LeetCode%201944.%E9%98%9F%E5%88%97%E4%B8%AD%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0%E7%9A%84%E4%BA%BA%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135416441" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-visible-people-in-a-queue/solutions/2592403/letmefly-1944dui-lie-zhong-ke-yi-kan-dao-e8p6/" target="_blank">LeetCode题解</a>|
631632
|1945.字符串转化后的各位数字之和|简单|<a href="https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/12/15/LeetCode%201945.%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E5%8C%96%E5%90%8E%E7%9A%84%E5%90%84%E4%BD%8D%E6%95%B0%E5%AD%97%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128335606" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/solutions/2021840/letmefly-1945zi-fu-chuan-zhuan-hua-hou-d-569r/" target="_blank">LeetCode题解</a>|
@@ -1026,7 +1027,6 @@
10261027
- [ ] [2874. 有序三元组中的最大值 II](https://leetcode.cn/problems/maximum-value-of-an-ordered-triplet-ii/) 补每日一题
10271028
- [ ] [368. 最大整除子集](https://leetcode.cn/problems/partition-equal-subset-sum/) 补每日一题(代码)
10281029
- [ ] [416. 分割等和子集](https://leetcode.cn/problems/partition-equal-subset-sum/) 补每日一题
1029-
- [ ] [2999. 统计强大整数的数目](https://leetcode.cn/problems/count-the-number-of-powerful-integers/) 补每日一题
10301030
- [ ] Readme(尤其是文章列表部分)自动生成而非半自动或手动输修改
10311031
- [ ] 有空玩下[这个](https://github.com/LetMeFly666/ViT-MGI/commit/df2255f07aa318d55f44da262315789a15f0f2fc)
10321032
- [ ] arknights主题不支持mermaid的渲染
@@ -1048,6 +1048,7 @@
10481048
- [x] [1037. 有效的回旋镖](https://leetcode.cn/problems/valid-boomerang/)也可写
10491049
- [x] [1845. 座位预约管理系统 ](https://leetcode.cn/problems/seat-reservation-manager/)
10501050
- [x] [2073. 买票需要的时间](https://leetcode.cn/problems/time-needed-to-buy-tickets/)
1051+
- [x] [2999. 统计强大整数的数目](https://leetcode.cn/problems/count-the-number-of-powerful-integers/) 补每日一题
10511052
- [x] 将域名修改为blog.letmefly.xyz,以使用Cloudflare代理,旧域名会重定向到新域名(的对应路径)
10521053
- [x] 自动发起pr
10531054

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
title: 1922.统计好数字的数目:乘法原理+快速幂
3+
date: 2025-04-14 00:31:36
4+
tags: [题解, LeetCode, 中等, 数学, 乘法原理, 快速幂]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1922.统计好数字的数目:乘法原理+快速幂
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-good-numbers/](https://leetcode.cn/problems/count-good-numbers/)
11+
12+
<p>我们称一个数字字符串是 <strong>好数字</strong> 当它满足(下标从 <strong>0</strong> 开始)<strong>偶数</strong> 下标处的数字为 <strong>偶数</strong> 且 <strong>奇数</strong> 下标处的数字为 <strong>质数</strong> (<code>2</code>,<code>3</code>,<code>5</code> 或 <code>7</code>)。</p>
13+
14+
<ul>
15+
<li>比方说,<code>"2582"</code> 是好数字,因为偶数下标处的数字(<code>2</code> 和 <code>8</code>)是偶数且奇数下标处的数字(<code>5</code> 和 <code>2</code>)为质数。但 <code>"3245"</code> <strong>不是</strong> 好数字,因为 <code>3</code> 在偶数下标处但不是偶数。</li>
16+
</ul>
17+
18+
<p>给你一个整数 <code>n</code> ,请你返回长度为 <code>n</code> 且为好数字的数字字符串 <strong>总数</strong> 。由于答案可能会很大,请你将它对<strong> </strong><code>10<sup>9</sup> + 7</code> <strong>取余后返回</strong> 。</p>
19+
20+
<p>一个 <strong>数字字符串</strong> 是每一位都由 <code>0</code> 到 <code>9</code> 组成的字符串,且可能包含前导 0 。</p>
21+
22+
<p> </p>
23+
24+
<p><strong>示例 1:</strong></p>
25+
26+
<pre>
27+
<b>输入:</b>n = 1
28+
<b>输出:</b>5
29+
<b>解释:</b>长度为 1 的好数字包括 "0","2","4","6","8" 。
30+
</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<pre>
35+
<b>输入:</b>n = 4
36+
<b>输出:</b>400
37+
</pre>
38+
39+
<p><strong>示例 3:</strong></p>
40+
41+
<pre>
42+
<b>输入:</b>n = 50
43+
<b>输出:</b>564908303
44+
</pre>
45+
46+
<p> </p>
47+
48+
<p><strong>提示:</strong></p>
49+
50+
<ul>
51+
<li><code>1 <= n <= 10<sup>15</sup></code></li>
52+
</ul>
53+
54+
55+
56+
## 解题方法:乘法原理+快速幂
57+
58+
每个偶数下标有5种选择,每个奇数下标有4种选择,每个元素之间的选择互补干扰冲突。
59+
60+
由于共有$a=\lfloor\frac{n+1}{2}\rfloor$个偶数位和$b=\lfloor\frac{n}{2}\rfloor$个奇数位,所以一共有$5^a4^b$种答案。
61+
62+
如何快速计算$m^n$?使用[快速幂](https://blog.letmefly.xyz/tags/%E5%BF%AB%E9%80%9F%E5%B9%82/)可在$\log n$的时间复杂度内求出。
63+
64+
快速幂原理方法请见:[这里](https://web.letmefly.xyz/Notes/ACM/Problems/%E5%BF%AB%E9%80%9F%E5%B9%82/)
65+
66+
+ 时间复杂度$O(\log n)$
67+
+ 空间复杂度$O(1)$
68+
69+
### AC代码
70+
71+
#### C++
72+
73+
```cpp
74+
/*
75+
* @Author: LetMeFly
76+
* @Date: 2025-04-13 17:00:20
77+
* @LastEditors: LetMeFly.xyz
78+
* @LastEditTime: 2025-04-13 17:06:07
79+
* @Description: AC,100.00%,51.97%
80+
*/
81+
typedef long long ll;
82+
const ll MOD = 1e9 + 7;
83+
84+
class Solution {
85+
private:
86+
ll pow(ll a, ll b) {
87+
ll ans = 1;
88+
while (b) {
89+
if (b & 1) {
90+
ans = ans * a % MOD;
91+
}
92+
a = a * a % MOD;
93+
b >>= 1;
94+
}
95+
return ans;
96+
}
97+
public:
98+
int countGoodNumbers(long long n) {
99+
return pow(5, (n + 1) / 2) * pow(4, n / 2) % MOD;
100+
}
101+
};
102+
```
103+
104+
#### Python
105+
106+
Python用户可以无视手动实现快速幂。
107+
108+
```python
109+
'''
110+
Author: LetMeFly
111+
Date: 2025-04-13 17:06:16
112+
LastEditors: LetMeFly.xyz
113+
LastEditTime: 2025-04-13 17:06:17
114+
'''
115+
MOD = 1000000007
116+
117+
class Solution:
118+
def countGoodNumbers(self, n: int) -> int:
119+
return pow(5, (n + 1) // 2, MOD) * pow(4, n // 2, MOD) % MOD
120+
```
121+
122+
#### Java
123+
124+
```java
125+
/*
126+
* @Author: LetMeFly
127+
* @Date: 2025-04-13 17:07:43
128+
* @LastEditors: LetMeFly.xyz
129+
* @LastEditTime: 2025-04-14 00:07:21
130+
* @Description: AC,100.00%,87.23%
131+
*/
132+
class Solution {
133+
private final long mod = 1000000007;
134+
135+
private long pow(long a, long b) {
136+
long ans = 1;
137+
while (b > 0) {
138+
if ((b & 1) == 1) {
139+
ans = ans * a % mod;
140+
}
141+
a = a * a % mod;
142+
b >>= 1;
143+
}
144+
return ans;
145+
}
146+
147+
public int countGoodNumbers(long n) {
148+
return (int)(pow(5, (n + 1) / 2) * pow(4, n / 2) % mod);
149+
}
150+
}
151+
```
152+
153+
#### Golang
154+
155+
```go
156+
/*
157+
* @Author: LetMeFly
158+
* @Date: 2025-04-13 17:54:42
159+
* @LastEditors: LetMeFly.xyz
160+
* @LastEditTime: 2025-04-14 00:05:17
161+
* @Description: continue on subway,太晃了还是先算了
162+
* @Description: AC,100.00%,63.16%
163+
*/
164+
package main
165+
166+
var MOD1922 = int64(1000000007)
167+
168+
func pow1922(a int64, b int64) int64 {
169+
ans := int64(1)
170+
for ; b > 0; b >>= 1 {
171+
if b & 1 == 1 {
172+
ans = ans * a % MOD1922
173+
}
174+
a = a * a % MOD1922
175+
}
176+
return ans
177+
}
178+
179+
func countGoodNumbers(n int64) int {
180+
return int(pow1922(5, (n + 1) / 2) * pow1922(4, n / 2) % MOD1922)
181+
}
182+
```
183+
184+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147200001)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/14/LeetCode%201922.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E6%95%B0%E5%AD%97%E7%9A%84%E6%95%B0%E7%9B%AE/)哦~
185+
>
186+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,9 @@ categories: [自用]
10931093
|||
10941094
|demurrage|n. (船舶)租借逾期费用,滞留|
10951095
|cripple|n. 跛子,残疾人,瘸子,伤残人<br/>v. 使残疾,使跛,使成瘸子,严重损坏|
1096+
|||
1097+
|voucher|n. 保证人,证件,教育补助券,消费券<br/>v. 证明,为...准备凭据,发给...凭证|
1098+
|deterioration|n. 恶化,退化,变质,堕落|
10961099

10971100
<p class="wordCounts">单词收录总数</p>
10981101

newSolution.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: LetMeFly
33
Date: 2022-07-03 11:21:14
44
LastEditors: LetMeFly.xyz
5-
LastEditTime: 2025-04-09 23:21:29
5+
LastEditTime: 2025-04-14 00:27:31
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
What's more: 代码结构写的很混乱 - 想单文件实现所有操作
@@ -60,9 +60,20 @@ def getPlatform():
6060
return 'MacOS'
6161
else:
6262
return 'Linux(or others)'
63-
issueCreateResult = os.popen(f'gh issue create -t "Who can add 1 more problem of LeetCode {num}" -b "By [newSolution.py](https://github.com/LetMeFly666/LeetCode/blob/{lastSHA}/newSolution.py) using GH on {getPlatform()} " -l "题解" -a "@me"').read()
64-
print(issueCreateResult)
65-
issueNum = int(issueCreateResult.split('\n')[0].split('/')[-1])
63+
issueTitle = f'Who can add 1 more problem of LeetCode {num}' # (#872)
64+
alreadyRelatedIssueLists = os.popen(f'gh issue list --search "{issueTitle}"').read()
65+
alreadyRelatedIssueListsSplited = alreadyRelatedIssueLists.split('\n')
66+
print(alreadyRelatedIssueLists)
67+
print(alreadyRelatedIssueListsSplited)
68+
issueNum = 0
69+
for line in alreadyRelatedIssueListsSplited:
70+
if issueTitle in line:
71+
issueNum = int(line.split()[0])
72+
print(issueNum)
73+
if not issueNum:
74+
issueCreateResult = os.popen(f'gh issue create -t "Who can add 1 more problem of LeetCode {num}" -b "By [newSolution.py](https://github.com/LetMeFly666/LeetCode/blob/{lastSHA}/newSolution.py) using GH on {getPlatform()} " -l "题解" -a "@me"').read()
75+
print(issueCreateResult)
76+
issueNum = int(issueCreateResult.split('\n')[0].split('/')[-1])
6677

6778
input('代码写完后按回车生成题解模板:')
6879

tryGH.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-14 00:20:48
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-14 00:26:53
6+
'''
7+
import os
8+
num = 192
9+
num = 1922
10+
11+
issueTitle = f'Who can add 1 more problem of LeetCode {num}'
12+
alreadyRelatedIssueLists = os.popen(f'gh issue list --search "{issueTitle}"').read()
13+
alreadyRelatedIssueListsSplited = alreadyRelatedIssueLists.split('\n')
14+
print(alreadyRelatedIssueLists)
15+
print(alreadyRelatedIssueListsSplited)
16+
issueNum = 0
17+
for line in alreadyRelatedIssueListsSplited:
18+
if issueTitle in line:
19+
issueNum = int(line.split()[0])
20+
print(issueNum)

0 commit comments

Comments
 (0)