当前位置:网站首页>Number theory blocking (integer division blocking)
Number theory blocking (integer division blocking)
2022-04-23 09:41:00 【Zimba_】
Preface :
Break for too long , I forgot how to blog ..
This blog is used to introduce a quick solution with integral Division .
Symbol description :
⌊ a b ⌋ \left \lfloor \frac{a}{b}\right \rfloor ⌊ba⌋ : a a a Divide b b b Rounding down .
⌈ a b ⌉ \left \lceil \frac{a}{b}\right \rceil ⌈ba⌉ : a a a Divide b b b Rounding up .
∑ i = 1 n f ( i ) \sum_{i=1}^{n}f(i) ∑i=1nf(i) : Cumulative sign , Express f ( 1 ) + f ( 2 ) + … + f ( n ) f(1)+f(2)+…+f(n) f(1)+f(2)+…+f(n).
problem :
solve ∑ i = 1 n ⌊ n i ⌋ \sum_{i=1}^{n}\left \lfloor \frac{n}{i}\right \rfloor ∑i=1n⌊in⌋, among 1 ⩽ n ⩽ 1 e 12 1\leqslant n \leqslant 1e12 1⩽n⩽1e12.
Text :
First , It's not hard to find one o(n) The violence of .
We need a n \sqrt{n} n How to do it .
Let's take an example , Let's take n = 10 n=10 n=10, Then enumerate from 1 1 1 To n n n enumeration i i i, Observe ⌊ n i ⌋ \left \lfloor \frac{n}{i}\right \rfloor ⌊in⌋ Value .
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10
10 10 10 5 5 5 3 3 3 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
That's what it looks like up there .
It's not hard to find or prove , The same number is continuous , It can be divided into segments with the same value . Then we can use this property , For each piece , Multiply the number of this block by the length of the block , You can solve the sum of this block .
There are still two problems to be solved .
One is Number of blocks , One is How to quickly find both ends of a block .
First, the number of blocks , about i i i from 1 1 1 To n n n Value , ⌊ n i ⌋ \left \lfloor \frac{n}{i}\right \rfloor ⌊in⌋ The number of values of will not exceed 2 n 2\sqrt{n} 2n, That is, the number of blocks will not exceed 2 n 2\sqrt{n} 2n.
It's easy to prove ,
Um. , It can be proved , front n \sqrt{n} n individual i i i At most n \sqrt{n} n Seed value , And when i i i Greater than n \sqrt{n} n when , The value must be less than n \sqrt{n} n, So no more than 2 n 2\sqrt{n} 2n( Excerpt from ztc Mouth paste ).
So the second question is , Quickly find the endpoint of the block .
When the left end point of a piece is l l l, So its right endpoint r r r Namely ⌊ n ⌊ n l ⌋ ⌋ \left \lfloor \frac{n}{\left \lfloor \frac{n}{l}\right \rfloor}\right \rfloor ⌊⌊ln⌋n⌋. This is even better proof .
It can also be interpreted as , It is known that i i i, Then with i i i The right endpoint of the same block is ⌊ n ⌊ n i ⌋ ⌋ \left \lfloor \frac{n}{\left \lfloor \frac{n}{i}\right \rfloor}\right \rfloor ⌊⌊in⌋n⌋
in other words , When i i i The value is l l l To r r r Between time , The result of the formula is the same , So just multiply by the length of the interval ( r − l + 1 ) (r-l+1) (r−l+1) that will do .
Finally, for the original problem , Upper template code :( If there is mold taking, add mold taking )
ll ans=0;
for(ll l=1,r;l<=n;l=r+1)
{
r=n/(n/l);
ans+=(r-l+1)*(n/l);
}
Examples and applications :
A few more examples , Yes , But not all , I still need to find more questions to do .
problem 1:
It is known that w w w and n n n, solve ∑ i = 1 n ⌊ w i ⌋ \sum_{i=1}^{n}\left \lfloor \frac{w}{i}\right \rfloor ∑i=1n⌊iw⌋, among 1 ⩽ n , w ⩽ 1 e 12 1\leqslant n,w \leqslant 1e12 1⩽n,w⩽1e12.
Explain :
Different from the original question , The upper bound of enumeration here n n n No longer as a divisor .
So when calculating the right endpoint r r r When , It will cause a detailed problem .
Look at the formula , r = w / ( w / l ) r=w/(w/l) r=w/(w/l). There are two special situations .
One is w / l = = 0 w/l==0 w/l==0, At this point, calling this formula again will lead to division 0 0 0 The situation of , Lead to RE.
The other is , The result of the calculation is r > n r>n r>n, At this time, the right endpoint is not within the boundary .
Take these two situations into account , Modify on the basis of the original code .
ll ans=0;
for(ll l=1,r;l<=n;l=r+1)
{
if(w/l)r=w/(w/l);
else r=n;
r=min(r,n);
ans+=(r-l+1)*(w/l);
}
problem 2:
It is known that a , b a,b a,b and n n n, solve ∑ i = 1 n ⌊ a i ⌋ ⌊ b i ⌋ \sum_{i=1}^{n}\left \lfloor \frac{a}{i}\right \rfloor\left \lfloor \frac{b}{i}\right \rfloor ∑i=1n⌊ia⌋⌊ib⌋, among 1 ⩽ n , a , b ⩽ 1 e 12 1\leqslant n,a,b \leqslant 1e12 1⩽n,a,b⩽1e12.
Explain :
Multiply the original problem by one more , But don't be afraid. , Just from the original 2 a 2\sqrt{a} 2a A breakpoint , Turned into 2 a + 2 b 2\sqrt{a}+2\sqrt{b} 2a+2b A breakpoint , Complexity is still at the root level .
We just need to find the nearest breakpoint every time .
ll ans=0;
for(ll l=1,r,r1,r2;l<=n;l=r+1)
{
if(a/l)r1=a/(a/l);
else r1=n;
if(b/l)r2=b/(b/l);
else r2=n;
r=min(r1,r2)
r=min(r,n);
ans+=(r-l+1)*(a/l)*(b/l);
}
problem 3:
It is known that w w w and n n n, solve ∑ i = 1 n ⌈ w i ⌉ \sum_{i=1}^{n}\left \lceil \frac{w}{i}\right \rceil ∑i=1n⌈iw⌉, among 1 ⩽ n , w ⩽ 1 e 12 1\leqslant n,w \leqslant 1e12 1⩽n,w⩽1e12.
Explain :
This is rounded up , It's hard to find the same conclusion as before , But we can convert rounding up to rounding down .
⌈ w i ⌉ = ⌊ w + i − 1 i ⌋ = ⌊ w − 1 i ⌋ + 1 \left \lceil \frac{w}{i}\right \rceil=\left \lfloor \frac{w+i-1}{i}\right \rfloor=\left \lfloor \frac{w-1}{i}\right \rfloor+1 ⌈iw⌉=⌊iw+i−1⌋=⌊iw−1⌋+1
It's easy to solve .
ll ans=0;
for(ll l=1,r;l<=n;l=r+1)
{
if((w-1)/l)r=(w-1)/((w-1)/l);
else r=n;
r=min(r,n);
ans+=(r-l+1)*((w-1)/l+1);
}
problem 5:
It is known that w w w and n n n, solve ∑ i = 1 n i ⌊ w i ⌋ \sum_{i=1}^{n}i\left \lfloor \frac{w}{i}\right \rfloor ∑i=1ni⌊iw⌋, among 1 ⩽ n , w ⩽ 1 e 12 1\leqslant n,w \leqslant 1e12 1⩽n,w⩽1e12.
Explain :
One more i i i, The difference from the original is , We can't take... This time r − l + 1 r-l+1 r−l+1 了 , front r − l + 1 r-l+1 r−l+1 The essence of is actually the interval and , What we're going to multiply by is the interval i i i Sum of .( Because in this interval ⌊ w i ⌋ \left \lfloor \frac{w}{i}\right \rfloor ⌊iw⌋ It's the same , Equivalent to a constant coefficient , That's why I can take it like this )
ll ans=0;
for(ll l=1,r;l<=n;l=r+1)
{
if(w/l)r=w/(w/l);
else r=n;
r=min(r,n);
ans+=(r-l+1)*(l+r)/2*(w/l);
}
problem 6: Module sum product
seek ∑ i = 1 n ∑ j = 1 m ( n m o d i ) × ( m m o d j ) , i ≠ j \sum_{i=1}^{n}\sum_{j=1}^{m}(n\;mod\;i)\times (m\;mod\;j),i\neq j ∑i=1n∑j=1m(nmodi)×(mmodj),i=j model 19940417 19940417 19940417 Value .
among 1 ⩽ n , m ⩽ 1 e 9 1\leqslant n,m \leqslant 1e9 1⩽n,m⩽1e9.
Explain :
The first thing to know is ( n m o d i ) = n − i × ⌊ w i ⌋ (n\;mod\;i)=n-i\times \left \lfloor \frac{w}{i}\right \rfloor (nmodi)=n−i×⌊iw⌋.
Then we start to change the formula , Be careful i ≠ j i\neq j i=j The condition of cannot be ignored .
After the formula is formulated , You can write .
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
const ll mod=19940417;
typedef pair<int,int> P;
const ll MAXN=1000000;
const ll inv6=3323403;
ll sum(ll l,ll r,ll flag)
{
if(flag==1)
{
return (r-l+1)*(l+r)/2%mod;
}
else
{
return (r*(r+1)%mod*(2*r+1)%mod*inv6%mod-(l-1)*l%mod*(2*l-1)%mod*inv6%mod+mod)%mod;
}
}
int main()
{
ll n,m;
scanf("%lld%lld",&n,&m);
ll sum1=0;
for(ll l=1,r;l<=n;l=r+1)
{
r=n/(n/l);
sum1=(sum1+n*(r-l+1)%mod-sum(l,r,1)*(n/l)%mod+mod)%mod;
}
ll sum2=0;
for(ll l=1,r;l<=m;l=r+1)
{
r=m/(m/l);
sum2=(sum2+m*(r-l+1)%mod-sum(l,r,1)*(m/l)%mod+mod)%mod;
}
ll nm=min(n,m);
ll sum3=0;
for(ll l=1,r,r1,r2;l<=nm;l=r+1)
{
if(n/l)r1=n/(n/l);
else r1=nm;
if(m/l)r2=m/(m/l);
else r2=nm;
r=min(r1,r2);
r=min(r,nm);
sum3=(sum3+n*m%mod*(r-l+1)%mod-(n*(m/l)%mod+m*(n/l)%mod)*sum(l,r,1)%mod+sum(l,r,2)*(n/l)%mod*(m/l)%mod+mod)%mod;
}
ll ans=(sum1*sum2-sum3+mod)%mod;
printf("%lld\n",ans);
return 0;
}
problem 7: Sum the remainder
Explain :
problem 6 Simplified version of , I won't post the code .
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621425537.html
边栏推荐
- 成功的DevOps Leader 应该清楚的3个挑战
- 501. Mode in binary search tree
- 《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
- High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
- 653. Sum of two IV - input BST
- Summary of common concepts and problems of linear algebra in postgraduate entrance examination
- 高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
- Kettle实验 (三)
- Summary of wrong questions 1
- 【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
猜你喜欢
Principle of synchronized implementation
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
Buuctf [actf2020 freshman competition] include1
AI上推荐 之 MMOE(多任务yyds)
112. Path sum
[SQL Server fast track] view and cursor of database
MySQL of database -- basic common query commands
Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
ABAP 7.4 SQL Window Expression
JS what is an event? Event three elements and operation elements
随机推荐
High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
Buuctf [actf2020 freshman competition] include1
【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
SAP debug debug for in, reduce and other complex statements
《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
PHP notes (I): development environment configuration
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
golang力扣leetcode 396.旋转函数
Kettle experiment
STM32 and FreeRTOS stack parsing
Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
Codeforces Round #784 (Div. 4)
Leetcode题库78. 子集(递归 c实现)
Three challenges that a successful Devops leader should be aware of
Flink 流批一体在小米的实践
SAP pi / PO soap2proxy consumption external WS example
Exclusive thoughts and cases of JS
ASUS laptop can't read USB and surf the Internet after reinstalling the system