当前位置:网站首页>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
边栏推荐
- Learn FPGA (from Verilog to HLS)
- SAP debug debug for in, reduce and other complex statements
- kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core
- 653. Sum of two IV - input BST
- SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
- SAP salv14 background output salv data can directly save files and send emails (with sorting, hyperlink and filtering format)
- Kettle实验 (三)
- MacOS下使用CLion编译调试MySQL8.x
- JS DOM learn three ways to create elements
- How to obtain geographical location based on photos and how to prevent photos from leaking geographical location
猜你喜欢

The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to

Set the maximum width of the body, but why does the background color of the body cover the whole page?

Node installation

Kettle experiment (III)

亚马逊云科技入门资源中心,从0到1轻松上云

MySQL of database -- overview and installation

Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“

Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie

501. 二叉搜索树中的众数

kettle实验
随机推荐
Kettle experiment conversion case
Introduction to sap pi / PO login and basic functions
Image processing in opencv -- Introduction to contour + contour features
Explanation of order and primitive root of number theory
ABAP 7.4 SQL Window Expression
Simple understanding of arguments in JS
[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
Pre parsing of JS
Flutter's loading animation is more interesting
Kettle experiment
Acquisition of DOM learning elements JS
Comparison of overloading, rewriting and hiding
Employee probation application (Luzhou Laojiao)
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
JS DOM event
Canary publishing using ingress
SQL used query statements
Exclusive thoughts and cases of JS
Node installation