[SGU]119. Magic Pairs

来源:互联网 发布:2017支付宝秒余额源码 编辑:程序博客网 时间:2024/05/17 08:50

Analysis

    这题要证明一个结论,答案就是所有的 (a0*i mod n,b0*i mod n) i=0..n-1,证明在草稿纸上懒得打出来……应当不难吧。

Accepted Code

type    lintpair=array[1..2] of longint;var    tot,n,i,a,b,x,y:longint;    ans:array[1..20000] of lintpair;function comp(a,b:lintpair):boolean;begin    if a[1]=b[1] then        comp:=a[2]<b[2]    else        comp:=a[1]<b[1];end;procedure sort(l,r:longint);var    i,j:longint;    mid,tmp:lintpair;begin    i:=l;    j:=r;    mid:=ans[(i+j) shr 1];    repeat        while comp(ans[i],mid) do            inc(i);        while comp(mid,ans[j]) do            dec(j);        if not (i>j) then        begin            tmp:=ans[i];            ans[i]:=ans[j];            ans[j]:=tmp;            inc(i);            dec(j);        end;    until i>j;    if l<j then        sort(l,j);    if i<r then        sort(i,r);end;begin    read(n,a,b);    a:=a mod n;    b:=b mod n;    tot:=1;    x:=a;    y:=b;    repeat        x:=(x+a) mod n;        y:=(y+b) mod n;        ans[tot][1]:=x;        ans[tot][2]:=y;        inc(tot);    until (x=a) and (y=b);    dec(tot);    writeln(tot);    sort(1,tot);    for i:=1 to tot do        writeln(ans[i][1],' ',ans[i][2]);end.