当前位置:网站首页>2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改, 可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同), 并使得所有数之和为X的倍数。
2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改, 可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同), 并使得所有数之和为X的倍数。
2022-07-28 03:08:00 【福大大架构师每日一题】
2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改,
可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同),
并使得所有数之和为X的倍数。
小红想知道,一共有多少种不同的修改方案。
1 <= N, X <= 10^5。
1 <= arr[i], P <= 10^9。
来自网易。
答案2022-07-27:
求所有数字的累加和sum。遍历,sum-[i]求次数,最后统计次数。
这道题关键在于发现数学规律。
时间复杂度:O(N)。
代码用rust编写。代码如下:
use rand::Rng;
fn main() {
let len: i64 = 100;
let value: i64 = 100;
let test_time: i32 = 100000;
println!("测试开始");
for _ in 0..test_time {
let n = rand::thread_rng().gen_range(0, len) + 1;
let mut arr = random_array(n, value);
let p = rand::thread_rng().gen_range(0, value) + 1;
let x = rand::thread_rng().gen_range(0, value) + 1;
let ans1 = ways1(&mut arr, p, x);
let ans2 = ways2(&mut arr, p, x);
if ans1 != ans2 {
println!("出错了!");
break;
}
}
println!("测试结束");
}
fn ways1(arr: &mut Vec<i64>, p: i64, x: i64) -> i64 {
let mut sum = 0;
for num in arr.iter() {
sum += *num;
}
let mut ans = 0;
for num in arr.iter() {
sum -= *num;
for v in 1..=p {
if v != *num {
if (sum + v) % x == 0 {
ans += 1;
}
}
}
sum += num;
}
return ans;
}
fn ways2(arr: &mut Vec<i64>, p: i64, x: i64) -> i64 {
let mut sum = 0;
for num in arr.iter() {
sum += *num;
}
let mut ans = 0;
for num in arr.iter() {
ans += cnt(p, x, *num, (x - ((sum - *num) % x)) % x);
}
return ans;
}
// 当前数字num
// 1~p以内,不能是num的情况下,% x == mod的数字有几个
// O(1)
fn cnt(p: i64, x: i64, num: i64, mod0: i64) -> i64 {
// p/x 至少有几个
// (p % x) >= mod ? 1 : 0
// 在不考虑变出来的数,是不是num的情况下,算一下有几个数,符合要求
let ans = p / x + if (p % x) >= mod0 {
1 } else {
0 } - if mod0 == 0 {
1 } else {
0 };
// 不能等于num!
return ans - if num <= p && num % x == mod0 {
1 } else {
0 };
}
// 为了测试
fn random_array(n: i64, v: i64) -> Vec<i64> {
let mut ans: Vec<i64> = vec![];
for _ in 0..n {
ans.push(rand::thread_rng().gen_range(0, v) + 1);
}
return ans;
}
执行结果如下:

边栏推荐
- Robot development -- lead screw and guide rail
- 叶子识别 颜色的特征提取 缺陷检测等
- How to solve the problem of win11 black desktop background?
- PCB丝印如何摆?请查收这份手册!
- 同时导出多个excel,并且一个excel中包含多个sheet
- Detailed tutorial of one click reinstallation of win7 system
- Redis经典面试题总结
- Redis persistence mechanism
- C语言实现动态版本的通讯录
- More than 50 interviews have been summarized, and notes and detailed explanations have been taken from April to June (including core test sites and 6 large factories)
猜你喜欢

⽇志分析⼯具(Splunk)

Uniapp——拨打电话、发送短信

How does win11 display fixed applications?

C -- switch case statement

C WinForm development: how to add pictures to project resources

SSM integration (integrated configuration)

C语言实现动态版本的通讯录

【下载文件】uniapp开发小程序,下载文件并保存到本地

Shell writing specifications and variables

When QML uses layout layout, a large number of < unknown file >: QML qquicklayoutattached: binding loop detected for property circular binding warnings appear
随机推荐
Uniapp - make phone calls and send text messages
数字孪生智慧楼宇可视化平台实现对园区企业、公众服务一体化
动画(animation)
Leetcode 208. implement trie (prefix tree) (2022.07.27)
Embedded database -- SQLite
Win11黑色桌面背景如何解决?
vi命令详解
每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
Defect detection of BP SVM system design of leaf defect detection
Win11怎么显示固定应用?
Win11如何重命名音频设备
Softek Barcode Reader 9.1.5
Win11输入法的选字框不见了怎么办?
[R language] environment specifies to delete RM function
Engineering Geology Practice - engineering geology problem set
vba批量读取sql的create文来创建表
Redis communication protocol -- resp protocol
When a dialog box pops up, the following form is not available
C -- switch case statement
Redis内存回收