当前位置:网站首页>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 :

边栏推荐
- Pypi counts the number of Downloads
- [pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
- Mp4 package analysis
- Details of happens before rules
- Go multiplexing
- [C language] program compilation (preprocessing)
- UDP message structure and precautions
- Cloud native platform, let edge applications play out!
- Execution methods with static code blocks and child and parent classes
- Summary and sorting of XSS (cross site script attack) related content
猜你喜欢

TS uses a third-party library, and there is no type declaration file error handling

Strategy mode, just read one article

Remote sensing image classification tool and visualization application of WebGIS

Flink's study notes

Vs2019 configuring Qt5 development environment

Generator set work arrangement problem code

Explorer TSSD 2019 software installation package download and installation tutorial

Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem

Computing network, AI first, shengteng AI helps operators' Digital Transformation -- work together to win-win Computing Era

B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry
随机推荐
Solution to the occupation of project startup port
Componentization and modularization
"Introduction to interface testing" punch in to learn day07: websocket interface: how to test a completely unfamiliar protocol interface?
Sword finger offer 11. rotate the minimum number of the array
Redux best practices "Redux toolkit"
Flink's study notes
C: wechat chat software instance (wpf+websocket+webapi+entityframework)
Go multiplexing
HAC cluster is modified to stand-alone
Sequence diagram of UML diagram series
[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
Details of happens before rules
SQL recursive follow-up
Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
Nuscenes data set summary
A bit of knowledge - websites about scripts
What are the basic skills of engineers? How to practice? -- Learning experience sharing "suggestions collection"
R language uses logistic regression, ANOVA, outlier analysis and visual classification iris iris data set
Wechat sports field reservation of applet completion works applet graduation design (8) graduation design thesis template