关于数论向上取整和向下取整知识整理

关于向上取整和向下取整知识整理

向下取整函数 f ( x ) = ⌊ x ⌋ f(x)=\lfloor x\rfloor f(x)=x 是单调递增的 ,向上取整函数 f ( x ) = ⌈ x ⌉ f(x)=\lceil x\rceil f(x)=x也是单调递增的。

对任意整数n,

⌈ n 2 ⌉ + ⌊ n 2 ⌋ = n \lceil \frac{n}{2}\rceil+\lfloor \frac{n}{2}\rfloor=n 2n+2n=n

⌈ n b ⌉ = ⌊ n + b − 1 b ⌋ ​ \lceil \frac{n}{b}\rceil=\lfloor \frac{n+b-1}{b}\rfloor​ bn=bn+b1

对任意 实数 x ≥ 0 x\ge0 x0整数 a , b > 0 a,b>0 a,b>0

⌈ ⌈ x / a ⌉ b ⌉ = ⌈ x a b ⌉ ​ \lceil \frac{\lceil x/a\rceil}{b}\rceil=\lceil \frac{x}{ab} \rceil​ bx/a=abx

⌈ ⌈ x / a ⌉ b ⌉ = ⌈ x a b ⌉ ​ \lceil \frac{\lceil x/a\rceil}{b}\rceil=\lceil \frac{x}{ab} \rceil​ bx/a=abx

⌈ a b ⌉ ≤ a + b − 1 b \lceil \frac{a}{b} \rceil\le \frac{a+b-1}{b} baba+b1

以上参考《算法导论》第三版

f n ( x ) = ⌈ n x ⌉ , x ∈ [ 1 , n ] f_n(x)=\lceil \frac{n}{x}\rceil,x\in[1,n] fn(x)=xn,x[1,n],那么 f n ( x ) f_n(x) fn(x)的结果有 O ( n ) O(\sqrt n) O(n )种,取法如下:

  1. i = n i=n i=n
  2. l a s t = ⌈ n ⌈ n / i ⌉ ⌉ ​ last=\lceil\frac{n}{\lceil n/i \rceil}\rceil​ last=n/in ,那么 ⌈ n i ⌉ ​ \lceil\frac{n}{i}\rceil​ in 是一个解,且该解对应的最小整数 l a s t ​ last​ last满足 f ( l a s t ) = ⌈ n i ⌉ ​ f(last)=\lceil\frac{n}{i}\rceil​ f(last)=in
  3. i = l a s t − 1 ​ i=last-1​ i=last1,如果 i > 0 ​ i>0​ i>0返回第二步

代码表示

int cdiv(int a,int b)// a/b的向上取整
{
    return (a+b-1)/b;
}
void work(int n)
{
    int tol=0;
    for(int i=n,last;i>0;i=last-1,tol++)
    {
        cout<<"f(x):"<<cdiv(n,i)<<endl;//print the solutions
        last=cdiv(n,cdiv(n,i));
    }
    cout<<"The number of solutions :"<<tol<<endl;
}
int main()
{
    int n;
    cin>>n;
    work(n);
}

f n ( x ) = ⌊ n x ⌋ , x ∈ [ 1 , n ] f_n(x)=\lfloor \frac{n}{x}\rfloor,x\in[1,n] fn(x)=xn,x[1,n],那么 f n ( x ) f_n(x) fn(x)的结果有 O ( n ) O(\sqrt n) O(n )种,取法如下:

  1. i = 1 i=1 i=1
  2. l a s t = ⌊ n ⌊ n / i ⌋ ⌋ last=\lfloor\frac{n}{\lfloor n/i \rfloor}\rfloor last=n/in,那么 ⌊ n i ⌋ \lfloor\frac{n}{i}\rfloor in 是一个解,且该解对应的最大整数 l a s t last last满足 f ( l a s t ) = ⌊ n i ⌋ f(last)=\lfloor\frac{n}{i}\rfloor f(last)=in
  3. i = l a s t + 1 i=last+1 i=last+1,如果 i < = n i<=n i<=n,则返回第二步

代码表示

int fdiv(int a,int b)
{
    return a/b;
}
void work(int n)
{
    int tol=0;
    for(int i=1,last;i<=n;i=last+1,tol++)
    {
        cout<<"f(x):"<<fdiv(n,i)<<endl;//print the solutions
        last=fdiv(n,fdiv(n,i));
    }
    cout<<"The number of solutions :"<<tol<<endl;
}
int main()
{
    int n;
    cin>>n;
    work(n);
}

你可能感兴趣的:(比赛技巧,#,数论杂项)