比赛 20141105 评测结果 AAAAAAAWWW
题目名称 韩信点兵 最终得分 70
用户昵称 黑駒 运行时间 0.028 s
代码语言 Pascal 内存使用 0.17 MiB
提交时间 2014-11-05 10:56:28
显示代码纯文本
var m,t,i,j,k,l,tot:longint;
  n:int64;
  ans,temp,fin,sum:int64;
  p:array[0..100] of longint;
  a:array[0..100] of longint;
  f:array[0..100] of int64;

begin
  assign(input,'HanXin.in');
  assign(output,'HanXin.out');
  reset(input);
  rewrite(output);
  ans:=1;
  readln(n,m);
  sum:=0;
  for i:=1 to m do
    begin
     readln(p[i],a[i]);
	 ans:=ans*p[i];
	end;
  for i:=1 to m do
    begin
	  tot:=1;
      temp:=ans div p[i];
      j:=1;
        while true do
        begin
        if (j>p[i]) then begin
          writeln('-1');
          close(input);
          close(output);
          halt;
          end;
          if (((temp*j) mod p[i])=0) then
          begin
            writeln('-1');
            close(input);
            close(output);
            halt; end;
          if (((temp*j) mod p[i])=1)
          then begin
            inc(sum,temp*j*a[i]);
            break;
            end;
         inc(j);
       end;
   end;
    if (sum>n) then begin
      writeln('-1');
      end
      else begin
      while (sum<=n) do
      begin
        inc(sum,ans);
      end;
      dec(sum,ans);
    writeln(n-sum);
    end;
    close(input);
    close(output);
end.