当前位置:网站首页>2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.
2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.
2022-07-25 02:44:00 【Fuda frame constructor daily question】
2022-07-19:f(i) : i All the factors of , Every factor is squared , Add up .
such as f(10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.
Give a number n, seek f(1) + f(2) + … + f(n).
n <= 10 Of 9 Power .
O(n) All methods will timeout ! Below it !
O( Radical sign N) Methods , It's over , A train of thought .
O(log N) Methods ,
From the Blue Bridge Cup exercises .
answer 2022-07-19:
Observation table , Dichotomy .
Time complexity O( Square root N + Square root N * logN).
The code to use rust To write . The code is as follows :
fn main() {
println!(" Beginning of the test ");
for i in 1..1000 {
if sum1(i) != sum2(i) {
println!(" Something went wrong {}", i);
}
}
println!(" End of test ");
}
// Violence method
fn sum1(n: i64) -> i64 {
let mut cnt: Vec<i64> = vec![];
for _ in 0..n + 1 {
cnt.push(0);
}
for num in 1..=n {
for j in 1..=num {
if num % j == 0 {
cnt[j as usize] += 1;
}
}
}
let mut ans = 0;
for i in 1..=n {
ans += i * i * cnt[i as usize];
}
return ans;
}
fn get_sqrt(n: i64) -> i64 {
let mut l: i64 = 1;
let mut r = n;
let mut m: i64;
let mut mm: i64;
let mut ans = 1;
while l <= r {
m = l + ((r - l) >> 1);
mm = m * m;
if mm == n {
return m;
} else if mm < n {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
return ans;
}
// Formal approach
// Time complexity O( Square root N + Square root N * logN)
fn sum2(n: i64) -> i64 {
// 100 -> 10
// 200 -> 14
let sqrt = get_sqrt(n);
let mut ans = 0;
for i in 1..=sqrt {
ans += i * i * (n / i);
}
// The second half
// Give you a number , Two points out several factors , At this number !
// By the maximum number ( Radical sign N), Start two
let mut k = n / (sqrt + 1);
while k >= 1 {
ans += sum_of_limit_number(n, k);
k -= 1;
}
return ans;
}
// Sum of squares formula n(n+1)(2n+1)/6
fn sum_of_limit_number(v: i64, n: i64) -> i64 {
let r = cover(v, n);
let l = cover(v, n + 1);
return ((r * (r + 1) * ((r << 1) + 1) - l * (l + 1) * ((l << 1) + 1)) * n) / 6;
}
fn cover(v: i64, n: i64) -> i64 {
let mut l = 1;
let mut r = v;
let mut m;
let mut ans = 0;
while l <= r {
m = (l + r) / 2;
if m * n <= v {
ans = m;
l = m + 1;
} else {
r = m - 1;
}
}
return ans;
}
The results are as follows :

边栏推荐
- Rolling division, Young's matrix and three-step flip
- Inheritance (prototype)
- Experienced the troubleshooting and solution process of an online CPU100% and the application of oom
- YouTube Download and (batch) Download
- [C language] program compilation (preprocessing)
- Apk packaging process
- Classic network learning RESNET code implementation
- Tp5.1 paging (with parameter transfer)
- Let's customize the loader
- Object.defineproperty use
猜你喜欢

Project management tool Zen

Classic network learning RESNET code implementation

Explanation of C language file operation function

Tp5.1 include include files (reference public files)

After six years of precipitation, new consumption has found a digital transformation paradigm

ICO objects in classification

Cloud native platform, let edge applications play out!

SQL Server 2022 installation

These 11 chrome artifacts are extremely cool to use

Custom types in C language
随机推荐
Can PostgreSQL CDC only connect to the main database? Connection from the library reports an error logical decoden
Picgo configuring Alibaba cloud OSS
Componentization and modularization
"Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
Request and response
Mp4 package analysis
Gbase 8s how to query relational databases in sentences to select sample syntax and results of data from complex types
Ctfshow misc introduction
Please ask a question: how to set the new table of MySQL CDC 2.2.x to only receive increment
Genesis, the world's first "consumption investment" public chain, was invited to participate in consensus 2022
Flink's study notes
DLL load failed: the page file is too small to complete the operation
Server performance monitoring
Go language path is relative, but relative Import paths are not supported in module mode
Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt
Ogg data extraction delay is large
Jedispoolconfig parameter configuration from the perspective of source code
Industrial control safety PLC firmware reverse II
On Calc optimization of calcite
Redis unauthorized access vulnerability recurrence (www.hetianlab.com)