向下取整函数 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+b−1⌋
对任意 实数 x ≥ 0 x\ge0 x≥0和整数 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 ⌈b⌈x/a⌉⌉=⌈abx⌉
⌈ ⌈ x / a ⌉ b ⌉ = ⌈ x a b ⌉ \lceil \frac{\lceil x/a\rceil}{b}\rceil=\lceil \frac{x}{ab} \rceil ⌈b⌈x/a⌉⌉=⌈abx⌉
⌈ a b ⌉ ≤ a + b − 1 b \lceil \frac{a}{b} \rceil\le \frac{a+b-1}{b} ⌈ba⌉≤ba+b−1
以上参考《算法导论》第三版
令 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)种,取法如下:
代码表示:
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)种,取法如下:
代码表示:
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);
}