Hướng dẫn giải của Hóa đơn tiền điệ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

#include <iostream>
#include <algorithm>
using namespace std;

long long calcVND(long long u)
{
    if (u <= 100) return u * 200;
    if (u <= 10000) return calcVND(100) + (u - 100) * 300;
    if (u <= 1000000) return calcVND(10000) + (u - 10000) * 500;
    return calcVND(1000000) + (u - 1000000) * 700;
}

long long calcKWh(long long u)
{
    if (calcVND(100) >= u) return u / 200;
    if (calcVND(10000) >= u) return 100 + (u - calcVND(100)) / 300;
    if (calcVND(1000000) >= u) return 10000 + (u - calcVND(10000)) / 500;
    return 1000000 + (u - calcVND(1000000)) / 700;
}

int main()
{
    long long x, y;
    cin >> x >> y;

    long long total = calcKWh(x), low = 0, high = total / 2, ans = -1;
    while (low <= high)
    {
        long long mid = (low + high) / 2;
        long long dif = calcVND(total - mid) - calcVND(mid);
        if (dif == y)
        {
            ans = mid; break;
        }
        if (dif < y) high = mid - 1;
        else low = mid + 1;
    }

    cout << calcVND(ans) << endl;
}

Code mẫu của happyboy99x

#include <cstdio>
typedef long long LL;

LL x, y;

int moneyTokWh(int money) {
    int res = 0;
    if( money > 497990000 ) {
        res += (money - 497990000)/700;
        money = 497990000;
    }
    if( money > 2990000 ) {
        res += (money - 2990000)/500;
        money = 2990000;
    }
    if( money > 20000 ) {
        res += (money - 20000)/300;
        money = 20000;
    }
    res += money/200;
    return res;
}

LL kWhToMoney( int kwh ) {
    if(kwh <= 100) return (LL) kwh * 200;
    if(kwh <= 10000) return (LL) 20000 + (kwh-100)*300;
    if(kwh <= 1000000) return (LL) 2990000 + (kwh-10000)*500;
    return (LL) 497990000 + (kwh-1000000)*700;
}

int main() {
    scanf("%lld%lld",&x,&y);
    x = moneyTokWh(x);
    int L = 0, H = x;
    while(L <= H) {
        int mid = (L+H)/2;
        int a = kWhToMoney(x-mid) - kWhToMoney(mid);
        if( a == y ) {
            printf("%lld\n",kWhToMoney(mid));
            return 0;
        }
        if( a < y ) H = mid-1;
        else L = mid+1;
    }
    return 0;
}

Code mẫu của ladpro98

program bill;
uses    math;
type    e=record
        val:int64;
        full:boolean;
        end;
const   fi='';
        lim:array[1..3] of longint = (100,10000,1000000);
        limP:array[1..3] of longint = (20000,2970000,495000000);
        p:array[1..4] of longint = (200,300,500,700);
var     x,y:int64;
        inp:text;

function KwhToMoney(a:int64):int64;
var     s:int64;
begin
        if a<=lim[1] then exit(p[1]*a);
        s:=lim[1]*p[1];
        dec(a,lim[1]);
        if a<=(lim[2]-lim[1]) then exit(s+p[2]*a);
        inc(s,(lim[2]-lim[1])*p[2]);
        dec(a,lim[2]-lim[1]);
        if a<=(lim[3]-lim[2]) then exit(s+p[3]*a);
        inc(s,(lim[3]-lim[2])*p[3]);
        dec(a,(lim[3]-lim[2]));
        exit(s+p[4]*a);
end;

function MoneyToKwh(a:int64):e;
var     s:int64;
        tp:e;
begin
        if a<=limP[1] then
        begin
                if a mod p[1]=0 then tp.full:=true
                else tp.full:=false;
                tp.val:=a div p[1];
                exit(tp);
        end;
        s:=lim[1];
        dec(a,limP[1]);
        if a<=limP[2] then
        begin
                if a mod p[2]=0 then tp.full:=true
                else tp.full:=false;
                tp.val:=s+a div p[2];
                exit(tp);
        end;
        s:=lim[2];
        dec(a,limP[2]);
        if a<=limP[3] then
        begin
                if a mod p[3]=0 then tp.full:=true
                else tp.full:=false;
                tp.val:=s+a div p[3];
                exit(tp);
        end;

        s:=lim[3];
        dec(a,limP[3]);
        if a mod p[4]=0 then tp.full:=true
                else tp.full:=false;
                tp.val:=s+a div p[4];
                exit(tp);
end;

procedure process;
var     l,r,t,m:int64;
        a,b:e;
begin
        l:=0;r:=x;
        while (l<=r) do
        begin
                m:=(l+r) div 2;
                a:=MoneyToKwh(m);
                b:=MoneyToKwh(m+y);
                t:=KwhToMoney(a.val+b.val);
                if t=x then
                if a.full and b.full then
                begin
                        write(m);
                        exit;
                end
                else    r:=m-1;
                if t<x then l:=m+1
                else    r:=m-1;
        end;
end;

begin
        assign(inp,fi);reset(inp);
        readln(inp,x,y);
        process;

end.

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;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp) { \
        if (REACHEOF) return 0;\
        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);

long long conv(long long x) {
    long long res = 0;
    res += min(x, 100LL) * 200;
    x -= 100;
    if (x <= 0) return res;

    res += min(x, 10000LL - 100) * 300;
    x -= 10000 - 100;
    if (x <= 0) return res;

    res += min(x, 1000000LL - 10000) * 500;
    x -= 1000000 - 10000;
    if (x <= 0) return res;

    res += x * 700;
    return res;
}

long long rev(long long x) {
    long long res = 0;
    if (x <= 100 * 200) return x / 200;
    res += 100;
    x -= 100 * 200;

    if (x <= (10000 - 100) * 300) return res + x / 300;
    res = 10000;
    x -= (10000 - 100) * 300;

    if (x <= (1000000 - 10000) * 500) return res + x / 500;
    res = 1000000;
    x -= (1000000 - 10000) * 500;

    return x / 700 + res;
}

int main() {
    long long x, y;
    while (cin >> x >> y) {
        long long sum = rev(x);

        FOR(i,1,sum/2) {
            long long a = conv(i);
            long long b = conv(sum-i);

            if (b-a == y) {
                cout << a << endl;
                break;
            }
        }
    }
    return 0;
}

Code mẫu của hieult

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

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --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 Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
    if (rsz <= 0) {
        ptr = 0;
        rsz = (int) fread(bf, 1, bfsz, stdin);
        if (rsz <= 0)
            return EOF;
    }
    --rsz;
    return bf[ptr++];
}
void ga(char &c) {
    c = EOF;
    while (!isalpha(c))
        c = gc();
}
int gs(char s[]) {
    int l = 0;
    char c = gc();
    while (isspace(c))
        c = gc();
    while (c != EOF && !isspace(c)) {
        s[l++] = c;
        c = gc();
    }
    s[l] = '\0';
    return l;
}
template<class T> bool gi(T &v) {
    v = 0;
    char c = gc();
    while (c != EOF && c != '-' && !isdigit(c))
        c = gc();
    if (c == EOF)
        return false;
    bool neg = c == '-';
    if (neg)
        c = gc();
    while (isdigit(c)) {
        v = v * 10 + c - '0';
        c = gc();
    }
    if (neg)
        v = -v;
    return true;
}

typedef pair<int, int> II;

const ld PI = acos(-1.0);
const ld eps = 1e-9;

const int dr[] = {0, +1, 0, -1};
const int dc[] = {+1, 0, -1, 0};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 7;

ll X, Y, sum;
ll f[10];
ll a[] = {100, 10000, 1000000};

ll cal(ll X){
    ll res = 0;
    if(X > f[2]){
        res = (X - f[2]) / 700 + 1000000;
    }
    else if(X > f[1]){
        res = (X - f[1]) / 500 + 10000;
    }
    else if(X > f[0]){
        res = (X - f[0]) / 300 + 100;
    }
    else res = X / 200;
    return res;
}

ll tinh(ll X){
    ll res = 0;
    if(X > a[2]){
        res = (X - a[2]) * 700 + f[2];
    }
    else if(X > a[1]){
        res = (X - a[1]) * 500 + f[1];
    }
    else if(X > a[0]){
        res = (X - a[0]) * 300 + f[0];
    }
    else res = X * 200;
    return res;
}

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

    cin >> X >> Y;
    f[0] = 100 * 200;
    f[1] = 9900 * 300 + f[0];
    f[2] = 990000 * 500 + f[1];

    sum = cal(X);
    ll ret, ret1;
    For(i, 1, 1000000){
        ret = tinh(i);
        ret1 = tinh(sum - i);
        if(ret1 - ret == Y){
            cout << ret;
            return 0;
        }
    }

    ll d = Y / 700;
    cout << tinh((sum - d) / 2);

    return 0;
}

Code mẫu của skyvn97

#include<stdio.h>
typedef unsigned long long ull;
ull x,y,l,m,r,a,b;
ull cost(ull a)
{
    if (a<=100) return (200*a);
    if (a<=10000) return (300*a-10000);
    if (a<=1000000) return (500*a-2010000);
    return (700*a-202010000);
}
int main(void)
{
    scanf("%llu",&x);
    scanf("%llu",&y);
    l=0;
    r=x;
    while (true)
          {
           if (l==r)              
              {
               if (cost(l)==x) {a=l;break;}
              }
           if (r-l==1)
              {
               if (cost(l)==x) {a=l; break;}
               if (cost(r)==x) {a=r; break;}
              }
           m=(l+r)/2;
           if (cost(m)==x)
              {
               a=m;
               break;
              }
           if (cost(m)>x) r=m-1;
           if (cost(m)<x) l=m+1;              
          }
    l=0;
    r=a/2;
    while (true)
          {
           if (l==r)
              {
               if (cost(a-l)-cost(l)==y) {b=l;break;}
              }
           if (r-l==1)
              {
               if (cost(a-l)-cost(l)==y) {b=l;break;}
               if (cost(a-r)-cost(r)==y) {b=r;break;}
              }
           m=(l+r)/2;
           if (cost(a-m)-cost(m)==y)
              {
               b=m;
               break;
              }
           if (cost(a-m)-cost(m)>y) l=m+1;
           if (cost(a-m)-cost(m)<y) r=m-1;
          }
    printf("%llu",cost(b));
}

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.