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

边栏推荐
- Remote sensing image classification tool and visualization application of WebGIS
- Four redis cluster schemes you must know and their advantages and disadvantages
- Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
- In the post deep learning era, where is the recommendation system going?
- Redis unauthorized access vulnerability recurrence (www.hetianlab.com)
- Go language path is relative, but relative Import paths are not supported in module mode
- Three ways to solve your performance management problems
- Please ask a question: how to set the new table of MySQL CDC 2.2.x to only receive increment
- YouTube Download and (batch) Download
- StrError and PERROR
猜你喜欢

Consul cluster deployment

Is redis'module'not an internal or external command?

Classic network learning RESNET code implementation

How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
It's still a synchronization problem

Wechat sports field reservation of the finished works of the applet graduation project (6) opening defense ppt

C language: Structure -- a detailed explanation of memory byte alignment

Tp5.1 include include files (reference public files)

B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry

String class
随机推荐
Please ask a question: how to set the new table of MySQL CDC 2.2.x to only receive increment
Gbase 8s how to query relational databases in sentences to select sample syntax and results of data from complex types
Simulation Implementation of string function (Part 1)
Insertion of balanced binary tree (AVL tree)
It's still a synchronization problem
Domain driven model (DDD)
Flume's study notes
What are the basic skills of engineers? How to practice? -- Learning experience sharing "suggestions collection"
YuQue - a useful tool for document writing and knowledge precipitation
Pycharm writes SQLite statements without code prompts
Common Oracle commands
Conceptual distinction between Po, Bo, VO, dto and POJO
Failed to create data snapshot: lock file [/siyuan/data/assets/image- 2022070216332-jijwccs.png failed: open /siyuan/data/assets/image- 2022070216332-jijwccs.png: permission denied; unable to lock fil
Picgo configuring Alibaba cloud OSS
JS utils tool function that makes you get twice the result with half the effort
On Calc optimization of calcite
Rolling division, Young's matrix and three-step flip
[C language] program compilation (preprocessing)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry