Hướng dẫn giải của Another Lucky Numbers


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 happyboy99x

#include<bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define TR(v,i) for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
const long long MAX = 1000000000000LL;
vector<long long> lucky, superLucky;

void generateLucky(int p, long long num) {
    if(num != 0) lucky.push_back(num);
    if(p < 12) {
        generateLucky(p+1, num * 10 + 4);
        generateLucky(p+1, num * 10 + 7);
    }
}

void generateSuperLucky(long long num, int pos) {
    if(num > 1) superLucky.push_back(num);
    for(int i = pos; i < (int) lucky.size(); ++i)
        if(num <= MAX / lucky[i])
            generateSuperLucky(num * lucky[i], i);
        else break;
}

int main() {
    ios::sync_with_stdio(false);
    generateLucky(0, 0);
    sort(all(lucky));
    generateSuperLucky(1, 0);
    sort(all(superLucky));
    superLucky.resize(unique(all(superLucky)) - superLucky.begin());
    int t; cin >> t;
    while(t--) {
        long long a, b; cin >> a >> b;
        cout << upper_bound(all(superLucky), b) - lower_bound(all(superLucky), a) << '\n';
    }
    return 0;
}

Code mẫu của ladpro98

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e5 + 7;
long long LIMIT = (long long)1e12 + 12;

vector<long long> lucky, luckyProd;
vector<long long> was[MOD];
queue<long long> Q;

void backtrack(int i, long long value) {
    if (value > 0) lucky.push_back(value);
    if (i == 0) return;
    backtrack(i - 1, value * 10 + 4);
    backtrack(i - 1, value * 10 + 7);
}

void initialize() {
    backtrack(12, 0);
    sort(lucky.begin(), lucky.end());
    Q.push(1); was[1].push_back(1);
    long long v, vv;
    while (!Q.empty()) {
        long long u = Q.front(); Q.pop();
        if (u > 1) luckyProd.push_back(u);
        for (auto it: lucky) {
            v = u * it, vv = v % MOD;
            if (v < LIMIT) {
                if (find(was[vv].begin(), was[vv].end(), v) == was[vv].end())
                    was[vv].push_back(v), Q.push(v);
            } else {
                break;
            }
        }
    }
    sort(luckyProd.begin(), luckyProd.end());
}

long long solve(long long till) {
    return upper_bound(luckyProd.begin(), luckyProd.end(), till) - luckyProd.begin();
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int nTest; cin >> nTest;
    const int T = 7778; long long A[T], B[T];
    for (int i = 0; i < nTest; ++i) cin >> A[i] >> B[i];
    LIMIT = *max_element(B, B + nTest) + 1;
    initialize();
    for (int i = 0; i < nTest; ++i)
        cout << solve(B[i]) - solve(A[i] - 1) << '\n';
    return 0;
}

Code mẫu của RR

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
const int BUFSIZE = (1<<12) + 17;
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp && !REACHEOF) { \
        memset(BUF,0,sizeof BUF);\
        int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
        if (inpzzz != BUFSIZE) REACHEOF = true;\
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);

int nx, nGood;
long long x[10111], good[1000111];

pair<int,int> solve(int l, int r) {
    if (l == r) {
        int first = nGood + 1;
        long long u = x[l];
        while (u > 0 && u <= 1000000000000LL) {
            good[++nGood] = u;
            u *= x[l];
        }
        return make_pair(first, nGood);
    }

    int mid = (l + r) >> 1;
    pair<int,int> left = solve(l, mid), right = solve(mid+1, r), res;

    res.first = left.first; res.second = right.second;

    FOR(i,left.first,left.second) {
        long long ln = 1000000000000LL / good[i];

        FOR(j,right.first,right.second) {
            if (good[j] > ln) break;
            good[++nGood] = good[i] * good[j];
        }
    }
    res.second = nGood;

    sort(good+res.first+1, good+res.second+1);
    return res;
}

void gen() {
    FOR(len,1,12) {
        REP(mask,1<<len) {
            long long now = 0;
            REP(i,len) {
                if (mask & (1<<i)) now = now * 10 + 7;
                else now = now * 10 + 4;
            }
            x[++nx] = now;
        }
    }
    sort(x+1, x+nx+1);

    solve(1, nx);
    nGood = unique(good+1, good+nGood+1) - good - 1;
}

int main() {
    gen();
    ios :: sync_with_stdio(false);
    int ntest; cin >> ntest;
    good[++nGood] = 1000000000000LL + 11;
    while (ntest--) {
        long long a, b; cin >> a >> b;
        cout << upper_bound(good+1, good+nGood+1, b) - lower_bound(good+1, good+nGood+1, a) << endl;
    }
    return 0;
}

Code mẫu của hieult

#include<cstdio>
#include<cmath>
#include<math.h>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<cctype>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<sstream>
#include<list>
#define fi first
#define se second
#define PB push_back
#define MP make_pair
#define ep 0.00001
#define oo 111111111
#define mod 1000000007
#define TR(c, it) for(typeof((c).begin()) it=(c).begin(); it!=(c).end(); it++)
#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 MS(a, x) memset(a, x, sizeof(a))
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(), a.end()
#define maxn 505
#define MAX 1000000000000ll
//#define g 9.81
double const PI=4*atan(1.0);

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;
typedef vector<II> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
typedef long long ll;
int dx[] = {+1, 0, -1, 0};
int dy[] = {0, -1, 0, +1};

void OPEN(){
    freopen("A.in","r",stdin);
    freopen("A.out","w",stdout);
}

typedef long long ll;

ll lucky[1 << 12 + 5], lsize;
set<long long> S;
vector<long long> V;

void duyet(int imax, long long A){
    if(A > MAX) return;
    //S.insert(A);
    if(A != 1) V.PB(A);
    for(int i = 0; i <= imax; i++){
        if(MAX / lucky[i] + 1 >= A)
            duyet(i, A * lucky[i]);
        else break;
    }
}

void init(){
    lsize = 0;
    lucky[lsize++] = 4; lucky[lsize++] = 7;
    rep(i, lsize){
        if(lucky[i] * 10 < MAX){
            lucky[lsize++] = lucky[i] * 10 + 4;
            lucky[lsize++] = lucky[i] * 10 + 7;
        }   
    }

    duyet(lsize - 1, 1);
}

int Unique(vector<long long> &X){
    X.PB(0);
    sort(ALL(X));
    X.resize(unique(ALL(X)) - X.begin());
    return SZ(X) - 1;
}

int main(){

   // OPEN();
    clock_t start = clock();
    init();
    int size = Unique(V);
    //cout << size << endl;
    //cout << V.size() << " " << V[V.size() - 1] << endl;
    int T;
    ll A, B;
    scanf("%d", &T);
    rep(run, T){
        scanf("%lld %lld", &A, &B);
        printf("%d\n", upper_bound(ALL(V), B) - upper_bound(ALL(V), A - 1));
    }
    //cout << clock() - start << endl;
}

Code mẫu của skyvn97

#include<cstdio>
#include<cstdlib>
#include<set>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll MAXV=(ll)1e12;
const int MAXD=12;
ll luc[10101];
ll pw[13]={1,10,100,1000,(ll)1e4,(ll)1e5,(ll)1e6,(ll)1e7,(ll)1e8,(ll)1e9,(ll)1e10,(ll)1e11,(ll)1e12};
ll a[400400];
set<ll> spl;
queue<ll> q;
int nluc,nsp;
void gen_luckynum(void) {
    int a[13];
    int nd;
    int i,j;
    nluc=0;
    ll val;
    for (nd=1;nd<=MAXD;nd++) {
        for (i=0;i<(1<<nd);i=i+1) {
            val=0;
            for (j=0;j<nd;j=j+1) {
                if ((i|(1<<j))==i) val+=7*pw[j];
                else val+=4*pw[j];
            }
            nluc++;
            luc[nluc]=val;
        }
    }
}
void gen_superluck(void) {
    spl.clear();
    while (!q.empty()) q.pop();
    nsp=0;
    q.push(1);
    ll x;
    int i;
    while (!q.empty()) {
        x=q.front(); q.pop();
        for (i=1;i<=nluc;i=i+1) {
            if (luc[i]>MAXV/x) break;
            if (spl.find(luc[i]*x)==spl.end()) {
                spl.insert(luc[i]*x);
                q.push(luc[i]*x);
                nsp++;
                a[nsp]=luc[i]*x;
            }
        }
    }
    sort(&a[1],&a[nsp+1]);
}
void answer(void) {
    int i,t,fi,li;
    scanf("%d",&t);
    ll f,l;
    for (i=1;i<=t;i=i+1) {
        scanf("%lld",&f);
        scanf("%lld",&l);
        if (a[nsp]<f) fi=nsp+1;
        else fi=lower_bound(&a[1],&a[nsp+1],f)-&a[0];
        if (a[nsp]<=l) li=nsp;
        else li=upper_bound(&a[1],&a[nsp+1],l)-&a[0]-1;
        printf("%d\n",li-fi+1);
    }
}
int main(void) {
    gen_luckynum();
    gen_superluck();
    answer();
    return 0;
}

Code mẫu của khuc_tuan

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

const long long LIMIT = 1000000000000LL;

long long list[1000010], ds[1000010];
int nlist, nds;

void duyet(int p, long long v) {
    if(p >= nlist) {
        if(v!=1) ds[nds++] = v;
        return;
    }
    if(v > LIMIT / list[p]) {
        if(v!=1) ds[nds++] = v;
        return;
    }
    for(int sl=0;;++sl) {
        duyet( p + 1, v);
        if(v > LIMIT / list[p]) break;
        v *= list[p];
    }
}

int main() {
    set<long long> se;
    se.insert(4);
    se.insert(7);
    bool tiep = true;
    while(tiep) {
        int os = se.size();
        for(set<long long> :: iterator p = se.begin(); p!=se.end();++p) {
            long long v = *p;
            if(v * 10 + 4 <= LIMIT) se.insert(v * 10 + 4);
            if(v * 10 + 7 <= LIMIT) se.insert(v * 10 + 7);
        }
        int ns = se.size();
        tiep = ns != os;
    }

    for(set<long long> :: iterator p = se.begin(); p != se.end(); ++p)
        list[nlist++] = *p;

    duyet(0, 1);
    sort( ds, ds + nds);
    nds = unique(ds, ds + nds) - ds;

    int st;
    scanf("%d", &st);
    for(int t=1;t<=st;++t) {
        long long A, B;
        cin >> A >> B;
        int res = upper_bound( ds, ds + nds, B) - lower_bound( ds, ds + nds, A);
        printf("%d\n", res);
    }   
    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.