当前位置:网站首页>1922. Count Good Numbers
1922. Count Good Numbers
2022-07-03 09:01:00 【wangjun861205】
A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime (2, 3, 5, or 7).
For example, “2582” is good because the digits (2 and 8) at even positions are even and the digits (5 and 2) at odd positions are prime. However, “3245” is not good because 3 is at an even index but is not even.
Given an integer n, return the total number of good digit strings of length n. Since the answer may be large, return it modulo 109 + 7.
A digit string is a string consisting of digits 0 through 9 that may contain leading zeros.
Example 1:
Input: n = 1
Output: 5
Explanation: The good numbers of length 1 are “0”, “2”, “4”, “6”, “8”.
Example 2:
Input: n = 4
Output: 400
Example 3:
Input: n = 50
Output: 564908303
Constraints:
- 1 <= n <= 1015
n 小于 10 的 15 次方, 所以 O(n)的方法就不用考虑了, 要往 O(logn)方向去想, 我们先来列一下前几个位数和数量的关系
1: 5
2: 20
3: 100
4: 400
5: 2000
6: 8000
7: 40000
8: 160000
其实这个过程就是重复 ×4 和 ×5 的过程(偶数位有 0, 2, 4, 6, 8 可选, 奇数位有 2, 3, 5, 7 可选), 从上面列出的关系我们还可以看出, 位数从 2 开始, 每增加一倍, 组合的数量就会成平方增长。这样我们就可以把 n 位拆分为多个 2 的 k 次方的位数, 例如 6 = 2² + 2¹, 6 位数字的组合数量就是 4 位数字的组合数量乘以 2 位数的组合数量, 从上面的排列也可以看出这点。
const M: i64 = 1000000007;
impl Solution {
pub fn count_good_numbers(n: i64) -> i32 {
if n == 1 {
return 5;
}
// 当前位数
let mut digits = 2;
// 当前组合数量
let mut count = 20i64;
// 如果当前位数小于目标位数
while digits < n {
// 位数成倍增长
let d = digits * 2;
// 组合数量成指数增长
let c = count.pow(2) % M;
// 如果n恰巧是2的k次方则直接返回计数
if d == n {
return c as i32;
}
// 如果增长后的位数大于目标位数则退出循环
if d > n {
break;
}
digits = d;
count = c;
}
// 如果当前位数等于目标位数则直接返回当前计数
if digits == n {
return count as i32;
}
// 当前位数的计数乘以剩余位数的计数
(count * Solution::count_good_numbers(n - digits) as i64 % M) as i32
}
}
边栏推荐
- Vscode编辑器右键没有Open In Default Browser选项
- [point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
- CATIA automation object architecture - detailed explanation of application objects (III) systemservice
- Jenkins learning (II) -- setting up Chinese
- [advanced feature learning on point clouds using multi resolution features and learning]
- 【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
- Jenkins learning (I) -- Jenkins installation
- Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
- Jenkins learning (III) -- setting scheduled tasks
- [point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
猜你喜欢
Install third-party libraries such as Jieba under Anaconda pytorch
Crawler career from scratch (IV): climb the bullet curtain of station B through API
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
On February 14, 2022, learn the imitation Niuke project - develop the registration function
Move anaconda, pycharm and jupyter notebook to mobile hard disk
2022-2-14 learning xiangniuke project - generate verification code
Modify idea code
LeetCode 535. Encryption and decryption of tinyurl
随机推荐
Flink学习笔记(八)多流转换
Trial of the combination of RDS and crawler
Utilisation de hudi dans idea
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
【点云处理之论文狂读经典版12】—— FoldingNet: Point Cloud Auto-encoder via Deep Grid Deformation
LeetCode 241. Design priorities for operational expressions
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
Computing level network notes
[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds
ERROR: certificate common name “*.” doesn’t match requested ho
Recommend a low code open source project of yyds
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
Detailed steps of windows installation redis
LeetCode每日一题(2090. K Radius Subarray Averages)
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
Serializer rewrite: update and create methods
Hudi 快速体验使用(含操作详细步骤及截图)
Internet Protocol learning record