Hướng dẫn giải của Pairs of Integers
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.
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 RR
//Written by RR {$ifdef rr} {$r+,q+} {$mode objfpc} {$inline off} {$else} {$r-,q-} {$mode objfpc} {$inline on} {$endif} uses math; const FINP = ''; FOUT = ''; var f1,f2 : text; n,sb,sc,ln : int64; nn : array[1..12] of longint; c : array[1..12] of longint; s : string[12]; test,count : longint; x : array[1..1024] of longint; lt10 : array[0..10] of int64; procedure openF; begin assign(f1,FINP); reset(f1); assign(f2,FOUT); rewrite(f2); end; procedure closeF; begin close(f1); close(f2); end; procedure init; var save:longint; begin ln:=0; save:=n; while (n>0) do begin inc(ln); nn[ln]:=n mod 10; n:=n div 10; end; n:=save; end; procedure duyet(i,nho:longint; done:boolean); var now,j,save:longint; begin if done then begin inc(sb,c[i-1]*lt10[i]); if (sb+sc=n) and (sc<sb) then begin inc(count); x[count]:=sb; end; c[i]:=nn[i]-c[i-1]-nho; if c[i]<0 then inc(c[i],10); inc(sc,c[i]*lt10[i]); if c[i-1]+c[i]+nho>9 then nho:=1 else nho:=0; if (i<ln) then duyet(i+1,nho,done); dec(sb,c[i-1]*lt10[i]); dec(sc,c[i]*lt10[i]); end else //not done for now:=0 to 9 do begin save:=nho; inc(sb,now*lt10[i]); if (sb+sc=n) and (sc<sb) then begin inc(count); x[count]:=sb; end; c[i]:=nn[i]-now-nho; if c[i]<0 then inc(c[i],10); inc(sc,c[i]*lt10[i]); if c[i]+now+nho>9 then nho:=1 else nho:=0; if now=c[i] then done:=false else done:=true; if i<ln then duyet(i+1,nho,done); dec(sb,now*lt10[i]); dec(sc,c[i]*lt10[i]); nho:=save; end; end; procedure swap(var a,b:longint); inline; var temp:longint; begin temp:=a; a:=b; b:=temp; end; procedure sort(l,r:longint); inline; var i,j,mid:longint; begin i:=l; j:=r; mid:=x[l+random(r-l+1)]; repeat while x[i]<mid do inc(i); while x[j]>mid do dec(j); if i<=j then begin swap(x[i],x[j]); inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if l<j then sort(l,j); end; procedure print; var i,j,save:longint; begin if count>0 then sort(1,count); save:=count; for i:=1 to count-1 do if x[i]=x[i+1] then dec(count); writeln(f2,count); for i:=1 to save do if x[i]<>x[i+1] then begin str(x[i],s); save:=length(s); str(n-x[i],s); while length(s)<save-1 do s:='0'+s; writeln(f2,x[i],' + ',s,' = ',n); end; end; begin openF; lt10[1]:=1; for count:=2 to 10 do lt10[count]:=lt10[count-1]*10; read(f1,test); for test:=1 to test do begin read(f1,n); init; sb:=0; sc:=0; count:=0; duyet(1,0,false); print; end; closeF; end.
Code mẫu của hieult
#include <cstdio> #include <set> //#include <conio.h> using namespace std; int sochuso(int N) { if(N==0) return 1; int t = 0; while(N!=0) {N/=10;t++;} return t; } void print(int X,int Y) { int n = sochuso(Y); int m = sochuso(X-Y); printf("%d + ",Y); for(int i = m+1;i<n;i++) printf("0"); printf("%d = %d\n",X-Y,X); } int main() { int test,n,a[11],X[33],u,v; long long mu10[11]; mu10[0] = 1; for(int i = 1;i<=10;i++) mu10[i] = mu10[i-1]*10; scanf("%d",&test); for(int itest=0;itest<test;itest++) { scanf("%d",&n); set <int> s; set <int>::iterator it; int so = 0,the = n,sl=0; while(the>0){a[++so]=the%10; the/=10;} for(int i = 0;i<=9;i++){ if((n-i)%11==0){ s.insert( (n/11)*10+i); break; } } if(n%2==0) { for(int i = 1;i<so;i++) { u = n/mu10[i]; v = n%mu10[i]; for(int j = 0;j<10;j++){ if((u-j)%11==0){ s.insert(((u/11)*10+j)*mu10[i]+v/2); } } if(u>1) for(int j = 0;j<10;j++){ if((u-1-j)%11==0){ s.insert((((u-1)/11)*10+j)*mu10[i]+mu10[i]/2+v/2); } } } } /* for(int i = 1;i<=sl;i++) for(int j = i+1;j<=sl;j++) if(X[i]>X[j]) { int temp = X[i]; X[i] = X[j]; X[j] = temp; } */ printf("%d\n",s.size()); for( it = s.begin();it!=s.end();it++) print(n,*it); } //getch(); }
Code mẫu của ll931110
Program QBINT; Const input = ''; output = ''; Var x,y,a,b: array[1..5000] of longint; n,t,i: longint; count: longint; num: longint; fi,fo: text; Procedure openfile; Begin Assign(fi, input); Reset(fi); Assign(fo, output); Rewrite(fo); End; Procedure solve; Var k,h,g,i: longint; p: int64; Begin Readln(fi, n); count:= 0; For k:= 0 to 9 do if (n - k) mod 11 = 0 then Begin inc(count); y[count]:= (n - k) div 11; x[count]:= n - y[count]; End; p:= 10; Repeat h:= n mod p; g:= (n - h) div p; If h = n then break; If not odd(h) then Begin For i:= 0 to 9 do If ((g - i) mod 11 = 0) and (g > i) then Begin inc(count); y[count]:= ((g - i) div 11) * p + h div 2; x[count]:= n - y[count]; End; dec(g); If g = 0 then break; h:= h + p; For i:= 0 to 9 do if ((g - i) mod 11 = 0) and (g > i) then Begin inc(count); y[count]:= ((g - i) div 11) * p + h div 2; x[count]:= n - y[count]; End; End; p:= p * 10; Until false; If not odd(n) then Begin p:= 10; While p <= n do Begin For i:= 1 to 9 do if (n >= i * p) and ((n - i * p) div 2 < p) then Begin inc(count); y[count]:= (n - i * p) div 2; x[count]:= n - y[count]; End; p:= p * 10; End; End; End; Procedure BubbleSort; Var i,j,tmp: longint; Begin For i:= 1 to count - 1 do For j:= i + 1 to count do if y[i] < y[j] then Begin t:= y[i]; y[i]:= y[j]; y[j]:= t; t:= x[i]; x[i]:= x[j]; x[j]:= t; End; If count >= 1 then Begin num:= 1; a[1]:= x[1]; b[1]:= y[1]; For i:= 2 to count do if x[i] <> a[num] then Begin inc(num); a[num]:= x[i]; b[num]:= y[i]; End; End else num:= 0; End; Procedure printresult; Var sa,sb: string; i,j: longint; Begin Writeln(fo, num); For i:= 1 to num do Begin str(a[i], sa); str(b[i], sb); Write(fo, sa, ' + '); For j:= length(sb) to length(sa) - 2 do write(fo, 0); Write(fo, sb, ' = '); Writeln(fo, n); End; End; Procedure closefile; Begin Close(fo); Close(fi); End; Begin openfile; Readln(fi, t); For i:= 1 to t do Begin solve; BubbleSort; printresult; End; closefile; End.
Code mẫu của khuc_tuan
#include <iostream> #include <map> #include <sstream> using namespace std; int n; long long pow10[10]; map<int, string> ma; void duyet(int pos, bool dx, bool kk, long long cur, long long x, int id) { if(cur > n) return; if(cur == n && dx && kk) { // printf("%d + %d = %d\n", x, n - x, n); if(!ma.count(x)) { ostringstream oss; oss << x; string s = oss.str(); s.erase(s.end() - id - 1); oss << " + " << s << " = " << n; ma[x] = oss.str(); } return; } if(pos == 10) return; if(pos >= 2 && cur / pow10[pos-2] % 10 != n / pow10[pos-2] % 10) return; for(int cs=0;cs<10;++cs) { if(dx) duyet( pos + 1, dx, cs != 0, cur + pow10[pos] * cs + pow10[pos-1] * cs, x + pow10[pos] * cs, id); else { duyet( pos + 1, dx, cs != 0, cur + pow10[pos] * 2 * cs, x + pow10[pos] * cs, id); duyet( pos + 1, true, cs != 0, cur + pow10[pos] * cs, x + pow10[pos] * cs, pos); } } } int main() { pow10[0] = 1; for(int i=1;i<10;++i) pow10[i] = 10 * pow10[i-1]; int st; scanf("%d", &st); for(int t=0;t<st;++t) { scanf("%d", &n); ma.clear(); duyet(0, 0, 0, 0, 0, 0); printf("%d\n", ma.size()); for(map<int,string> :: iterator p = ma.begin(); p!=ma.end(); ++p) printf("%s\n", p -> second . c_str() ); } return 0; }
Bình luận