Hướng dẫn giải của Số lẻ hoàn toàn


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 p:array[1..7] of longint=(1,10,100,1000,10000,100000,1000000);
var n,i,j,l,m,x:longint;
    a,s,u:string;
    code:integer;
    b:array[1..200000] of longint;

procedure gen(z,i:longint);
var j:longint;
begin
     if i>z then
     begin
          m:=m+1; b[m]:=x;
          if z=7 then
          begin
               m:=m+1; b[m]:=x+10000000;
          end;
          exit;
     end;
     for j:=1 to 5 do
     begin
          x:=x+p[i]*(j*2-1);
          gen(z,i+1);
          x:=x-p[i]*(j*2-1);
     end;
end;

function chk(a:string):boolean;
var i:longint;
begin
     for i:=1 to length(a) do
         if a[i]<'2' then exit(false);
     exit(true);
end;

function kt(x:longint):boolean;
begin
     while x>0 do
     begin
          if not odd(x mod 10) then exit(false);
          x:=x div 10;
     end;
     exit(true);
end;

function chk2(x:longint):boolean;
var i:longint;
begin
     for i:=1 to m do
         if (b[i]<x) and kt(x-b[i]) then exit(true);
     exit(false);
end;

begin
     for i:=1 to 7 do
     begin
          x:=0; gen(i,1);
     end;
     while true do
     begin
          readln(s);
          if s='[END]' then break;
          if s='[CASE]' then
          begin
               readln(n);
               if odd(n) then inc(n);
               str(n,a);
               l:=length(a);
               if chk(a) or chk2(n) then
               begin
                    writeln(n); continue;
               end;
               for i:=l downto 2 do
                   if a[i]<'2' then
                   begin
                        u:=copy(a,1,i-1);
                        val(u,x,code);
                        if chk(u) or chk2(x) then
                        begin
                             write(u);
                             for j:=i to l do write(2);
                             writeln;
                             break;
                        end;
                   end;
          end;
     end;
end.

Code mẫu của ladpro98

#include <bits/stdc++.h>
const int M = 1000006;
using namespace std;
int a[M];
int m;

int main() {
    ios :: sync_with_stdio(0); 
    for(int i = 1; i <= 9; i += 2) a[++m] = i;
    int cut = 1;
    for(int i = 2; i <= 8; i++) {
        int tmp = m;
        for(int j = cut; j <= tmp; j++)
            for(int k = 1; k <= 9; k += 2) 
                a[++m] = 10 * a[j] + k;
        cut = tmp;
    }
    string ins; int x;
    while (1) {
        cin >> ins;
        if (ins[1] == 'E') break;
        cin >> x;
        int j = lower_bound(a + 1, a + 1 + m, x) - a;
        int res = a[m] + a[m];
        for(int i = 1; i <= j; i++) {
            while (j > i && a[i] + a[j - 1] >= x) j--;
            if (a[i] + a[j] >= x) res = min(res, a[i] + a[j]);
        }
        cout << res << endl;
    }
    return 0;
}

Code mẫu của RR

#include <iostream>
#include <algorithm>

#define FOR(i,a,b)  for(int i=a; i<=b; i++)
#define FORD(i,a,b) for(int i=a; i>=b; i--)
using namespace std;

int n,a[500111],sl;
char s[20];

void dequy(int i,int gh,int now) {
    for(int u=1; u<=9; u+=2) {
        int x=now*10+u;
        if (i<gh) dequy(i+1,gh,x);
        else a[++sl]=x;
    }
}

void init() {
    FOR(l,1,8) dequy(1,l,0);
}

void solve() {
    int u=lower_bound(a+1,a+sl+1,n)-a-1; if (u<sl) u++;

    int res=a[u]+a[u]+1;
    a[0]=-a[sl]-1;
    int j=u;
    FOR(i,1,u) {
        if (i>j) break;
        if (a[i]>res) break;
        while (a[i]+a[j-1]>=n) j--;
        while (a[i]+a[j]>res) j--;
        if (a[i]+a[j]>=n) res=min(res,a[i]+a[j]);
    }
    printf("%d\n",res);
}

int main() {
    init();
    scanf("%s\n",&s);
    while (s[1]=='C') {
        scanf("%d",&n);
        solve();
        scanf("%s\n",&s);
    }
    return 0;
}

Code mẫu của hieult

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

    int k[] = {0,1,3,5,7,9};
    int mu5[10],a[500000],mu10[10],b[100000];

int main()
{
    //freopen("TOTALODD.in","r",stdin);

        mu5[0] = 1; mu10[0] = 1;
    for(int i = 1;i<=9;i++)
    {
        mu5[i] = mu5[i-1]*5;
        mu10[i] = mu10[i-1]*10;
    }
    for(int i = 1;i<=5;i++)
    {
        a[i] = k[i];
        b[i] = k[i];
    }
    int chay = 5;
    for(int i = 2;i<=8;i++)
    {
         int t,K;
         for(int j = 1;j<=mu5[i];j++)
         {
              t = a[(j-1)/mu5[i-1]+1];
              a[chay+j] = t*mu10[i-1]+b[(j-1)%mu5[i-1]+1];
         }
         if(i!=8)
              for(int j = 1;j<=mu5[i];j++)
                   b[j] = a[chay+j];
         chay = chay+mu5[i];
    }
    char s[20];
    int n;
    //printf("%d",chay);
    while(scanf("%s",s)>0 && s[1] != 'E')
    {
         scanf("%d",&n);
         int u,v,r,min = 100000001;
         if(n>=a[chay])
             u = chay;
         else
         {
             u = 1;v = chay;
             while(v-u >1)
             {
                 r = (u+v)/2;
                 if(a[r]<=n)
                     u = r;
                 else v = r;
             }
         }
         v = 1;
         while(v<=u)
         {
             while(a[v]+a[u]<n) v++;
             if(a[v]+a[u]<min)
                 min = a[v]+a[u];
             u--;
         }
         printf("%d\n",min);
    }
    //getch();
}

Code mẫu của khuc_tuan

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <queue>

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 fi first
#define se second
#define pb push_back
#define MP make_pair

typedef pair<int,int> PII;
typedef vector<int> VI;

class RepresentableNumbers {
public:
  int getNext(int);
};

int cs[55];
int nc;
int F[55][10][2][2][2];

int go(int pos,int need,int gr,int k1,int k2){
  if(pos == -1) {
    if(need != 0 || (!k1) || (!k2)) return -2;
    else return 0;
  }
  int&ret=F[pos][need][gr][k1][k2];
  if(ret!=-1)return ret;
  // need = need * 10 + cs[pos];
  ret=-2;
  for(int ad=0;ad<10;++ad) if(ad>=cs[pos] || gr){
    int cur = need * 10 + ad;
    for(int x1=k1?1:0;x1<=9;x1+=2){
      for(int x2=k2?1:0;x2<=9;x2+=2){
      int s = x1+x2;    
      if(s == cur || s + 1 == cur){
        int tmp = go(pos-1,cur-s,gr || ad>cs[pos],k1||x1>0,k2||x2>0);
        if(tmp!=-2){
          int z = ad;
          Rep(ii,pos)z*=10;
          if(ret == -2 || ret > z + tmp)
            ret = z + tmp;
        }
      }
      if(x2==0)x2--;
      }
      if(x1==0)x1--;
    }    
  }
  return ret;
}

int RepresentableNumbers::getNext(int X) {
  nc=0;
  while(X>0){
    cs[nc++]=X%10;
    X/=10;
  }
  memset(F,-1,sizeof(F));
  int tmp = go(nc-1,0,0,0,0);
  if(tmp!=-2)return tmp;
  int t=1;
  Rep(i,nc)t*=10;
  return t;
}

RepresentableNumbers run;

int main() {
    while(true){
        string s;
        cin>>s;
        if(s=="[END]")break;
        int n;
        cin>>n;
        cout<< run.getNext(n) << endl;
    }       
}

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.