码迷,mamicode.com
首页 > 其他好文 > 详细

HAOI2008题解

时间:2016-11-11 23:21:23      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:adl   处理   sqrt   type   haoi2008   记忆   zoj   repeat   noip   

又来写题解辣~然而并不太清楚题目排列情况。。。不管辣先写起来~

T1:[bzoj1041]

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1041

一个赤果果的数学题~~~

首先坐标轴上一定有四个,

最大公约数搞一下然后判断一下就可以啦,细节全部都在代码~

代码如下:

 1 var i,j,ans,d:longint;
 2     t,r,m:int64;
 3 function flag(x,y:longint):longint;
 4 var t:longint;
 5 begin
 6   if (x<y) then
 7   begin
 8     t:=x;
 9     x:=y;
10     y:=t;
11   end;
12   x:=x mod y;
13   if (x=0) then exit(y) else exit(flag(x,y));
14 end;
15 begin
16   readln(r);
17   ans:=0;
18   t:=2*r;
19   for d:=1 to trunc(sqrt(t)) do
20     if (t mod d=0) then
21     begin
22       for i:=1 to trunc(sqrt(r/d)) do
23       begin
24         j:=trunc(sqrt(t/d-i*i));
25         if (j=sqrt(t/d-i*i)) and (i<>j) and (flag(i,j)=1) then inc(ans);
26       end;
27       for i:=1 to trunc(sqrt(d/2)) do
28       begin
29         j:=trunc(sqrt(d-i*i));
30         if (j*j=d-i*i) and (i<>j) and (flag(i,j)=1) then inc(ans);
31       end;
32     end;
33   ans:=ans*4+4;
34   writeln(ans);
35 end.

T2:[bzoj1042]

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1042

又一个数学题hhh

先动态规划预处理,然后容斥原理就可以了。

代码如下:

 1 var i,j,k,l,tot,g:longint;
 2     c,s:array[1..4] of longint;
 3     f:array[0..100010] of int64;
 4     ans:int64;
 5 function f1(x:longint):int64;
 6 begin
 7   if (x>=0) then exit(f[x]) else exit(0);
 8 end;
 9 begin
10   fillchar(c,sizeof(c),0);
11   fillchar(f,sizeof(f),0);
12   readln(c[1],c[2],c[3],c[4],tot);
13   f[0]:=1;
14   for i:=1 to 4 do
15     for j:=c[i] to 100000 do
16       f[j]:=f[j]+f[j-c[i]];
17   for i:=1 to tot do
18   begin
19     fillchar(s,sizeof(s),0);
20     for j:=1 to 4 do
21       read(s[j]);
22     readln(g);
23     ans:=f[g];
24     for j:=1 to 4 do
25       ans:=ans-f1(g-(s[j]+1)*c[j]);
26     for j:=1 to 3 do
27       for k:=j+1 to 4 do
28         if (j<>k) then
29           ans:=ans+f1(g-(s[j]+1)*c[j]-(s[k]+1)*c[k]);
30     for j:=1 to 2 do
31       for k:=j+1 to 3 do
32         for l:=k+1 to 4 do
33           if (j<>k) and (k<>l) and (j<>l) then
34             ans:=ans-f1(g-(s[j]+1)*c[j]-(s[k]+1)*c[k]-(s[l]+1)*c[l]);
35     ans:=ans+f1(g-(s[1]+1)*c[1]-(s[2]+1)*c[2]-(s[3]+1)*c[3]-(s[4]+1)*c[4]);
36     writeln(ans);
37   end;
38 end.

 

(T3先留好坑。。。之后补)

T4:[bzoj1044]

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1044

第一问就是NOIP2015的day2T1(把我坑死的QAQQQ),

第二问就是动态规划就行了。

代码如下:

 1 var n,m,i,j,k,ans,left,right,max,anss,s,now1,x,y:longint;
 2     l,sl,ty:array[0..50000] of longint;
 3     f,f1:array[0..1,-1..50000] of longint;
 4 function tryit(x:longint):boolean;
 5 var i,j,s,left,right:longint;
 6 begin
 7   i:=0;
 8   j:=0;
 9   now1:=0;
10   while (i<n) do
11   begin
12     left:=i+1;
13     right:=n;
14     i:=(left+right) div 2;
15     while (right-left>1) do
16     begin
17       if (sl[i]-sl[now1]<=x) then left:=i else right:=i;
18       i:=(left+right) div 2;
19     end;
20     if (sl[right]-sl[now1]<=x) then i:=right else i:=left;
21     if (sl[n]-sl[now1]>x) then inc(j);
22     now1:=i;
23   end;
24   if (j>m) then exit(false) else exit(true);
25 end;
26 begin
27   read(n,m);
28   fillchar(l,sizeof(l),0);
29   fillchar(sl,sizeof(sl),0);
30   s:=0;
31   max:=0;
32   for i:=1 to n do
33   begin
34     read(l[i]);
35     sl[i]:=sl[i-1]+l[i];
36     if (l[i]>max) then max:=l[i];
37     s:=s+l[i];
38   end;
39   left:=max;
40   right:=s;
41   ans:=(left+right) div 2;
42   while (right-left>1) do
43   begin
44     if not(tryit(ans)) then left:=ans else right:=ans;
45     ans:=(left+right) div 2;
46   end;
47   ans:=right;
48   if tryit(left) then ans:=left;
49   write(ans, );
50   fillchar(f,sizeof(f),0);
51   fillchar(ty,sizeof(ty),0);
52   fillchar(f1,sizeof(f1),0);
53   anss:=0;
54   f[1,0]:=1;
55   for i:=0 to n do
56     f1[1,i]:=1;
57   k:=0;
58   for j:=1 to n do
59   begin
60     while (sl[j]-sl[k]>ans) do inc(k);
61     ty[j]:=k;
62   end;
63   for i:=1 to m+1 do
64   begin
65     x:=i mod 2;
66     y:=1-x;
67     for j:=0 to n do
68     begin
69       f[y,j]:=(f1[x,j-1]-f1[x,ty[j]-1]+10007) mod 10007;
70       f1[y,j]:=(f[y,j]+f1[y,j-1]) mod 10007;
71     end;
72     anss:=(anss+f[y,n]) mod 10007;
73   end;
74   writeln(anss);
75 end.

T5:[bzoj1045]

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1045

这题。。。标准的组合数学嘛~~~

随便搞一下就粗来了。。。QAQ

辣么辣么多数学题QAQQQ

代码如下:

 1 var n,i,j:longint;
 2     sum,ave,ans:int64;
 3     a,p:array[0..1000000] of longint;
 4 procedure qsort(lx,rx:longint);
 5 var i,j,m,x:longint;
 6 begin
 7   i:=lx;
 8   j:=rx;
 9   m:=p[(i+j) div 2];
10   repeat
11     while (p[i]<m) do inc(i);
12     while (p[j]>m) do dec(j);
13     if (i<=j) then
14     begin
15       x:=p[i];
16       p[i]:=p[j];
17       p[j]:=x;
18       inc(i);
19       dec(j);
20     end;
21   until (i>j);
22   if (i<rx) then qsort(i,rx);
23   if (j>lx) then qsort(lx,j);
24 end;
25 begin
26   readln(n);
27   fillchar(a,sizeof(a),0);
28   fillchar(p,sizeof(p),0);
29   sum:=0;
30   for i:=1 to n do
31   begin
32     readln(a[i]);
33     sum:=sum+a[i];
34   end;
35   ave:=sum div n;
36   for i:=2 to n do
37     p[i]:=p[i-1]-ave+a[i];
38   qsort(1,n);
39   ans:=0;
40   for i:=1 to n do
41     ans:=ans+abs(p[i]-p[n div 2+1]);
42   writeln(ans);
43 end.

T6:[bzoj1054]

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1054

QAQ这题是一个赤果果的BFS。。。

不对。。。

BFS要判重。。。这题判重都不要hhh

代码如下:

 1 type arr=array[1..4,1..4] of longint;
 2 const modp=10000009;
 3     tx:array[1..4] of longint=(1,-1,0,0);
 4     ty:array[1..4] of longint=(0,0,1,-1);
 5 var i,j,k,s,t,s1,s2,rx,ry,swap,rh:longint;
 6     q:array[0..65536*2] of arr;
 7     mark:array[0..65536*2] of boolean;
 8     step:array[0..65536*2] of longint;
 9     ans:arr;
10     ch:char;
11 function hash(x:arr):longint;
12 var i,j,ans,now:longint;
13 begin
14   ans:=0;
15   now:=1;
16   for i:=1 to 4 do
17     for j:=1 to 4 do
18     begin
19       ans:=(ans+int64(now*x[i,j]));
20       now:=(now+now);
21     end;
22   exit(ans);
23 end;
24 begin
25   s:=0;
26   t:=1;
27   fillchar(q,sizeof(q),0);
28   fillchar(ans,sizeof(ans),0);
29   for i:=1 to 4 do
30   begin
31     for j:=1 to 4 do
32     begin
33       read(ch);
34       while (ch<>0) and (ch<>1) do read(ch);
35       q[0,i,j]:=ord(ch)-48;
36     end;
37   end;
38   for i:=1 to 4 do
39   begin
40     for j:=1 to 4 do
41     begin
42         read(ch);
43         while (ch<>0) and (ch<>1) do read(ch);
44         ans[i,j]:=ord(ch)-48;
45     end;
46   end;
47   s1:=hash(q[s]);s2:=hash(ans);
48   mark[s1]:=true;
49   fillchar(step,sizeof(step),0);
50   if (s1=s2) then writeln(0) else
51   begin
52     while (s<t) do
53     begin
54       for i:=1 to 4 do
55         for j:=1 to 4 do
56           if (q[s,i,j]>0) then
57           begin
58             for k:=1 to 4 do
59             begin
60               rx:=i+tx[k];ry:=j+ty[k];
61               if  (1<=rx) and (rx<=4) and (1<=ry) and (ry<=4) and (q[s,rx,ry]=0) then
62               begin
63                 swap:=q[s,i,j];q[s,i,j]:=q[s,rx,ry];q[s,rx,ry]:=swap;
64                 rh:=hash(q[s]);             
65               if not(mark[rh]) then
66                 begin
67                   if (rh=s2) then
68                   begin
69                     writeln(step[s]+1);
70                     halt;
71                   end;
72                   mark[rh]:=true;
73                   q[t]:=q[s];
74                   step[t]:=step[s]+1;
75                   inc(t);
76                 end;
77                 swap:=q[s,i,j];q[s,i,j]:=q[s,rx,ry];q[s,rx,ry]:=swap;
78               end;
79               end;
80             end;
81         inc(s);
82         end;
83     end;
84 end.

T7:[bzoj1055]

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1055

又一个动态规划呢!

不过是一个记忆化搜索。。。

代码如下:

 1 const ch:array[1..4] of char=(W,I,N,G);
 2 var i,j:longint;
 3     f,flag:array[1..4,1..200,1..200] of boolean;
 4     x:array[1..4,1..16,1..2] of char;
 5     a:array[1..4] of longint;
 6     s:string;
 7     flagg:boolean;
 8 function calc(x:char):longint;
 9 begin
10   if (x=W) then exit(1)
11   else if (x=I) then exit(2)
12   else if (x=N) then exit(3)
13   else if (x=G) then exit(4);
14 end;
15 function f1(k,left,right:longint):boolean;
16 var i,j,t1,t2:longint;
17 begin
18   if flag[k,left,right] then exit(f[k,left,right]);
19   flag[k,left,right]:=true;
20   if (right=left) and (s[left]=ch[k]) then
21   begin
22     f[k,left,right]:=true;
23     exit(true);
24   end;
25   for i:=1 to a[k] do
26   begin
27     t1:=calc(x[k,i,1]);
28     t2:=calc(x[k,i,2]);
29     for j:=left to right-1 do
30       if f1(t1,left,j) and f1(t2,j+1,right) then
31       begin
32         f[k,left,right]:=true;
33         exit(true);
34       end;
35   end;
36   f[k,left,right]:=false;
37   exit(false);
38 end;
39 begin
40   fillchar(a,sizeof(a),0);
41   for i:=1 to 4 do
42     read(a[i]);
43   readln;
44   fillchar(f,sizeof(f),false);
45   fillchar(flag,sizeof(flag),false);
46   for j:=1 to 4 do
47     for i:=1 to a[j] do
48       readln(x[j,i,1],x[j,i,2]);
49   readln(s);
50   flagg:=false;
51   for i:=1 to 4 do
52     if f1(i,1,length(s)) then
53     begin
54       write(ch[i]);
55       flagg:=true;
56     end;
57   if not(flagg) then write(The name is wrong!);
58   writeln;
59 end.

(T8的坑也先留着。。。QAQQQ)

终于写完啦~~~撒花撒花~~~

 

HAOI2008题解

标签:adl   处理   sqrt   type   haoi2008   记忆   zoj   repeat   noip   

原文地址:http://www.cnblogs.com/Tommyr7/p/6055703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!