当前位置:网站首页>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
}
}
边栏推荐
- Spark 结构化流写入Hudi 实践
- Jenkins learning (III) -- setting scheduled tasks
- STM32F103 can learning record
- Principles of computer composition - cache, connection mapping, learning experience
- Flink学习笔记(十)Flink容错机制
- 2022-1-6 Niuke net brush sword finger offer
- Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
- 【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
- 用Redis实现分布式锁
- Solve POM in idea Comment top line problem in XML file
猜你喜欢

Redis learning (I)

LeetCode 324. Swing sort II

What are the stages of traditional enterprise digital transformation?

Liteide is easy to use

Apply for domain name binding IP to open port 80 record
![[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu](/img/57/b413a93a456a1872fc19aa825c937a.jpg)
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
![[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds](/img/7d/b66545284d6baea2763fd8d8555e1d.png)
[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds

CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers

Digital statistics DP acwing 338 Counting problem

【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
随机推荐
LeetCode 871. Minimum refueling times
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Recommend a low code open source project of yyds
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points
Uc/os self-study from 0
Build a solo blog from scratch
Linxu learning (4) -- Yum and apt commands
图像修复方法研究综述----论文笔记
Idea uses the MVN command to package and report an error, which is not available
Win10 quick screenshot
【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis
Computing level network notes
Bert install no package metadata was found for the 'sacraments' distribution
Word segmentation in full-text indexing
Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
Spark structured stream writing Hudi practice
【点云处理之论文狂读经典版9】—— Pointwise Convolutional Neural Networks
Simple use of MATLAB
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字