Description
一个房间里有n盏灯泡,一开始都是熄着的,有1到n个时刻,每个时刻i,我们会将i的倍数的灯泡改变状态(即原本开着的现将它熄灭,原本熄灭的现将它点亮),问最后有多少盏灯泡是亮着的。
Input
一个数n
Output
m,表示最后有m盏是亮着的
Sample Input
5
Sample Output
2
Data Constraint
Hint
范围:40%的数据保证,n<=maxlongint
100%的数据保证,n<=10^200
正解高精度开方,然而那东西很烦,所以我不用了
想要看高精度开方百度,不过我不太确定看完会不会吐的更久……
思路:
直接二分查找
(二分你跟我说不会那你还是别搞OI了呗……)
不论是二分,还是开方,都是码农题,吐……
代码:
var
n,l,r,mid:ansistring;
function add(s1,s2:ansistring):ansistring;
var
a,b,c:array[0..10000]of longint;
n,i,l1,l2,l3,x:longint;
s:string;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0);
x:=0;
l1:=length(s1);
for i:=1 to l1 do a[l1-i+1]:=ord(s1[i])-ord('0');
l2:=length(s2);
for i:=1 to l2 do b[l2-i+1]:=ord(s2[i])-ord('0');
i:=1;
while (i<=l1)or(i<=l2) do
begin
c[i]:=a[i]+b[i]+x;
x:=c[i] div 10;
c[i]:=c[i] mod 10;
inc(i);
end;
if x>0 then
begin
l3:=i;
c[i]:=x;
end
else l3:=i-1;
add:='';
for i:=l3 downto 1 do
add:=add+chr(c[i]+48);
end;
function time(n1,n2:ansistring):ansistring;
var
a,b,c:array[0..10000]of longint;
lena,lenb,lenc,i,j,x:longint;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0);
lena:=length(n1);
lenb:=length(n2);
for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-ord('0');
for i:=1 to lena do
begin
x:=0;
for j:=1 to lenb do
begin
c[i+j-1]:=a[i]*b[j]+x+c[i+j-1];
x:=c[i+j-1]div 10;
c[i+j-1]:=c[i+j-1]mod 10;
end;
c[i+j]:=x;
end;
lenc:=lena+lenb;
while (c[lenc]=0)and(lenc>1)do dec(lenc);
time:='';
for i:=lenc downto 1 do
time:=time+chr(c[i]+48);
end;
function division(s:ansistring;y:longint):ansistring;
var
a,b,c:array[0..10000]of longint;
lena,lenb,lenc,i,j,x:longint;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0);
lena:=length(s);
for i:=1 to lena do a[lena-i+1]:=ord(s[i])-ord('0');
x:=0;
for i:=lena downto 1 do
begin
x:=x*10+a[i];
c[i]:=x div y;
x:=x mod y;
end;
lenc:=lena;
division:='';
while (c[lenc]=0)and(lenc>1)do dec(lenc);
for i:=lenc downto 1 do
division:=division+chr(c[i]+48);
end;
function smaller(s1,s2:ansistring):boolean;
begin
if (length(s1)or(length(s1)=length(s2))and((s1<=s2))then
exit(true)
else exit(false);
end;
function bigger(s1,s2:ansistring):boolean;
begin
if (length(s1)>length(s2))or(length(s1)=length(s2))and((s1>s2))then
exit(true)
else exit(false);
end;
function judge(s:ansistring):boolean;
begin
if smaller(time(s,s),n) and bigger(time(add(s,'1'),add(s,'1')),n)then
exit(true)
else exit(false);
end;
begin
readln(n);
l:='1';
r:=n;
mid:=division(add(l,r),2);
while true do
begin
if judge(mid) then
begin
writeln(mid);
halt;
end;
if bigger(time(mid,mid),n)then
begin
r:=mid;
mid:=division(add(l,r),2);
end
else
if smaller(time(mid,mid),n)then
begin
l:=add(mid,'1');
mid:=division(add(l,r),2);
end;
end;
end.
你看完吐了没?