delit.pas 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var
  2. inp : file of char;
  3. chislo : array [1..1000] of byte;
  4. num,pos : word;
  5. function GetByte(i:word):byte;
  6. begin
  7. GetByte:=(chislo[i div 8 + 1] and (1 shl (8 - i mod 8)));
  8. end;
  9. procedure AddByte(bt:char);
  10. begin
  11. if (bt='1') then
  12. chislo[pos div 8 + 1] := chislo[pos div 8 + 1] or (1 shl (8 - pos mod 8));
  13. inc(pos);
  14. end;
  15. Function GetPair(i:word):byte;
  16. var ret:byte;
  17. begin
  18. ret:=0;
  19. if GetByte(i) <> 0 then ret := 2;
  20. if GetByte(i+1) <> 0 then inc(ret);
  21. GetPair:=ret;
  22. end;
  23. Function IfModul(l,r:word):boolean;
  24. var i:word;flag:boolean;sum:integer;
  25. begin
  26. i := r-l+1;
  27. i := i and 1;
  28. if (i<>0) then
  29. begin
  30. i:=1;
  31. sum := GetByte(l);
  32. if (sum<>0) then sum:=1;
  33. end
  34. else
  35. begin
  36. sum:=GetPair(l);
  37. i:=2;
  38. end;
  39. flag:=false;
  40. while (l+i)<>(r+1) do
  41. begin
  42. if flag then sum := sum + GetPair(i+l)
  43. else sum := sum - GetPair(i+l);
  44. flag := not flag;
  45. inc(i,2);
  46. end;
  47. while sum > 0 do sum := sum-5;
  48. if (sum=0) then ifModul:=true
  49. else ifModul:=false;
  50. end;
  51. var
  52. ch:char;
  53. i,j:word;
  54. count : longint;
  55. flag:boolean;
  56. BEGIn
  57. Assign(inp,'num.txt');
  58. Reset(inp);
  59. pos:=1;
  60. num:=0;
  61. while not EOF(inp) do
  62. begin
  63. read(inp,ch);
  64. if flag or (ch<>'0') then
  65. begin
  66. flag := true;
  67. Addbyte(ch);
  68. inc(num);
  69. end
  70. end;
  71. count:=0;
  72. for i := 1 to num-2 do
  73. for j := i+2 to num do
  74. if IfModul(i,j) then inc(count);
  75. Write('Число подстрок, кратных 5: ',count);
  76. ReadLn;
  77. END.