Hướng dẫn giải của Tablica


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

const fi='';
      fo='';
var n,k,dem:longint;
    re:array[1..1000] of integer;
    a:array[1..1000] of longint;
    b,cur:array[1..1000,0..1] of integer;

procedure rf;
var i:longint;
begin
     assign(input,fi);
     reset(input);
     readln(n,k);
     for i:=1 to k do
     begin
          readln(a[i],b[i,0],b[i,1]);
          cur[i,0]:=(a[i]+n-1) div n;
          cur[i,1]:=a[i] mod n;
          if cur[i,1]=0 then cur[i,1]:=n;
     end;
     close(input);
end;

procedure rotate(z,val,now:longint);
var i:longint;
begin
     if val=0 then exit;
     dem:=dem+val;
     for i:=now to k do
         if cur[i,z]=cur[now,z] then
         begin
              cur[i,1-z]:=(cur[i,1-z]+val) mod n;
              if cur[i,1-z]=0 then cur[i,1-z]:=n;
         end;
end;

procedure pr;
var i:longint;
begin
     for i:=1 to k do
     begin
          dem:=0;
          if cur[i,1]<=b[i,1] then rotate(0,b[i,1]-cur[i,1],i)
          else rotate(0,b[i,1]+n-cur[i,1],i);
          if cur[i,0]<=b[i,0] then rotate(1,b[i,0]-cur[i,0],i)
          else rotate(1,b[i,0]+n-cur[i,0],i);
          re[i]:=dem;
     end;
end;

procedure wf;
var i:longint;
begin
     assign(output,fo);
     rewrite(output);
     for i:=1 to k do writeln(re[i]);
     close(output);
end;

begin
     rf;
     pr;
     wf;
end.

Code mẫu của happyboy99x

#include<cstdio>

#define K 1000

int x[K], y[K], dx[K], dy[K], n, k;

void enter() {
    scanf("%d%d",&n,&k);
    for(int i = 0; i < k; ++i) {
        int v; scanf("%d%d%d",&v,dx+i,dy+i);
        --v; --dx[i]; --dy[i];
        x[i] = v / n; y[i] = v % n;
    }
}

void solve() {
    for(int i = 0; i < k; ++i) {
        int mov = (dy[i] - y[i] + n * n) % n;
        for(int j = i; j < k; ++j)
            if(x[j] == x[i]) y[j] = (y[j] + mov) % n;
        int mov2 = (dx[i] - x[i] + n * n) % n;
        for(int j = i; j < k; ++j)
            if(y[j] == y[i]) x[j] = (x[j] + mov2) % n;
        printf("%d\n", mov + mov2);
    }
}

int main() {
    enter();
    solve();
    return 0;
}

Code mẫu của RR

{$R+,Q+}
uses math;
const
  FINP='';
  FOUT='';
  MAXN=1001;
var
  f1,f2:text;
  sum,n,k:longint;
  u1,v1,u2,v2:array[1..MAXN] of longint;
procedure openF;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
end;
procedure closeF;
begin
  close(f1);
  close(f2);
end;
procedure inp;
var
  i,x:longint;
begin
  read(f1,n,k);
  for i:=1 to k do
    begin
      read(f1,x,u2[i],v2[i]);
      u2[i]-=1; v2[i]-=1; x-=1;
      u1[i]:=x div n; v1[i]:=x mod n;
    end;
end;
procedure solve;
var
  i,j,u,v:longint;
begin
  for i:=1 to k do
    begin
      if u1[i]<u2[i] then u:=u2[i]-u1[i]
      else if u1[i]>u2[i] then u:=n-u1[i]+u2[i]
      else u:=0;
      if v1[i]<v2[i] then v:=v2[i]-v1[i]
      else if v1[i]>v2[i] then v:=n-v1[i]+v2[i]
      else v:=0;
      writeln(f2,u+v);
      for j:=i to k do
      if u1[i]=u1[j] then
        begin
          v1[j]+=v;
          if v1[j]>=n then v1[j]-=n;
        end;
      for j:=i to k do
      if v1[i]=v1[j] then
        begin
          u1[j]+=u;
          if u1[j]>=n then u1[j]-=n;
        end;
    end;
end;
begin
  openF;
  inp;
  solve;
  closeF;
end.

Code mẫu của hieult

#include <stdio.h>
//#include <conio.h>

main()
{
      int n,k,x,a1[1001],a2[1001],b1[1001],b2[1001];
      scanf("%d %d",&n,&k);
      for(int i=1;i<=k;i++)
      {
              scanf("%d %d %d",&x,&b1[i],&b2[i]);
              a1[i]=(x-1)/n+1;
              a2[i]=(x-1)%n+1;
      }
      for(int i=1;i<=k;i++)
      {
              int u=(b1[i]+n-a1[i])%n;
              int v=(b2[i]+n-a2[i])%n;
              printf("%d\n",u+v);
              for(int j=i;j<=k;j++)
                   if(a1[j]==a1[i])
                          a2[j]=(a2[j]+v-1)%n+1;
              for(int j=i;j<=k;j++)
                   if(a2[j]==a2[i])
                          a1[j]=(a1[j]+u-1)%n+1;
      }
     // getch();
}

Code mẫu của ll931110

{$MODE DELPHI}
program TABLIC;
const
  fin = '';
  fou = '';
  maxn = 10000;
  maxk = 1000;
var
  n,k: integer;
  fi,fo: text;
  a,dx,dy,posx,posy: array[1..maxk] of integer;

procedure openfile;
begin
  assign(fi, fin);
    reset(fi);

  assign(fo, fou);
    rewrite(fo);
end;

procedure solve;
var
  i,j,nx,ny: integer;
begin
  readln(fi, n, k);
  for i := 1 to k do readln(fi, a[i], dx[i], dy[i]);
  for i := 1 to k do
    begin
      posy[i] := a[i] mod n;
      if posy[i] = 0 then posy[i] := n;
      posx[i] := a[i] div n;
      if posy[i] <> n then inc(posx[i]);
    end;

  for i := 1 to k do
    begin
      nx := dy[i] - posy[i];
      if nx < 0 then nx := nx + n;
      ny := dx[i] - posx[i];
      if ny < 0 then ny := ny + n;

      writeln(fo, nx + ny);

      for j := 1 to k do
        if posx[j] = posx[i] then
          begin
            posy[j] := posy[j] + nx;
            if posy[j] > n then posy[j] := posy[j] - n;
          end;

      for j := 1 to k do
        if posy[j] = posy[i] then
          begin
            posx[j] := posx[j] + ny;
            if posx[j] > n then posx[j] := posx[j] - n;
          end;
    end;
end;

procedure closefile;
begin
  close(fo);
  close(fi);
end;

begin
  openfile;
  solve;
  closefile;
end.

Code mẫu của khuc_tuan

#include <iostream>

#include <iomanip>

#include <fstream>

#include <set>

#include <map>

#include <queue>

#include <deque>

#include <stack>

#include <cmath>

#include <cstring>

#include <string>

#include <sstream>

#include <algorithm>

#include <functional>

#include <ctime>

#include <cstdio>

#include <cstdarg>



using namespace std;



#define Rep(i,n) for(int i=0;i<(n);++i)

#define For(i,a,b) for(int i=(a);i<=(b);++i)

#define Ford(i,a,b) for(int i=(a);i>=(b);--i)

#define Fit(i,v) for(__typeof(v.begin()) i=v.begin();i!=v.end();++i)

#define Fill(a,b) memset( (a), (b), sizeof(a))

#define pb push_back

#define MP make_pair

#define fi first

#define se second



typedef pair<int,int> PII;

typedef vector<int> VI;

typedef long long LL;



int nextInt() { int x; scanf("%d", &x); return x; }

#define ni nextInt()

int n,k;
vector<PII> step;

void process(PII ins,int &i,int &j){
    if(ins.fi>=0){
        if(i==ins.fi) j=(j+ins.se)%n;
    }
    else
        if(j==(-ins.fi-1)) i=(i+ins.se)%n;
}

int main() {
    n=ni,k=ni;

    Rep(tt,k){
        int res=0;
        int x=ni-1;
        int r=ni-1;
        int c=ni-1;
        int i=x/n;
        int j=x%n;
        Rep(k,step.size())process(step[k],i,j);
        if(j!=c) step.pb(MP(i,(c+n-j)%n)),res+=(c+n-j)%n;
        if(i!=r) step.pb(MP(-c-1,(r+n-i)%n)),res+=(r+n-i)%n;
        cout<<res<<endl;
    }
    return 0;
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.