95pts求调
  • 板块P1763 埃及分数
  • 楼主Benty
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/7/25 11:28
  • 上次更新2025/7/25 16:23:44
查看原帖
95pts求调
930277
Benty楼主2025/7/25 11:28
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
using ull = long long;

const double eps = 1e-10;
const int N = 1e6 + 10;

ull a, b;
ull vans[N], vnow[N];
ull nowp, ansp;

ull gcd(ull a, ull b) {
    if (a < 0) a = -a;
    if (b < 0) b = -b;
    if (a < b) swap(a, b);
    if (b == 0) return a;
    return gcd(b, a % b);
}

bool Lessthan(ull xa, ull xb, ull ya, ull yb) { return xa * yb < ya * xb; }

bool Notok(ull a, ull b, ull i, ull cnt) { 
    return cnt * (1.0 / i) < (double)a / b; 
}

void dfs(ull step, ull a, ull b, ull lastb, ull maxstep, ull maxb) {
    if (step > maxstep || a == 0) return;
    
    ull g = gcd(a, b);
    a /= g;
    b /= g;
    
    if (step == maxstep - 1) {
        if (a != 1 || b <= lastb) return;
        vnow[step] = b;
        if (ansp == 0 || b < vans[step]) {
            memcpy(vans + 1, vnow + 1, step * sizeof(ull));
            ansp = step;
        }
        return;
    }
    
    if (step == maxstep - 2) {
        ull min_k = 4 * b / (a * a) + 1;
        ull max_k = min(2 * maxb / a, maxb * (maxb - 1) / b);
        
        for (ull k = min_k; k <= max_k; ++k) {
            ull delta = k * k * a * a - 4 * k * b;
            if (delta <= 0) continue;
            
            ull s = sqrt(delta);
            if (s * s != delta) continue;
            
            ull y = (k * a + s) / 2;
            ull x = (k * a - s) / 2;
            
            if (x <= lastb || y <= lastb) continue;
            if (x >= y) continue;  // 保证x < y
            
            if (ansp == 0 || y < vans[step + 1]) {
                memcpy(vans + 1, vnow + 1, (step - 1) * sizeof(ull));
                vans[step] = x;
                vans[step + 1] = y;
                ansp = step + 1;
            }
        }
        return;
    }
    
    ull start = max(lastb + 1, (b + a - 1) / a);  // 向上取整(b/a)
    ull end = (maxstep - step + 1) * b / a;
    end = min(end, maxb);
    
    for (ull i = start; i <= end; ++i) {
        if (Notok(a, b, i, maxstep - step)) break;
        
        ull newa = a * i - b;
        ull newb = b * i;
        ull g = gcd(newa, newb);
        newa /= g;
        newb /= g;
        
        vnow[step] = i;
        dfs(ste[测试情况](https://cdn.luogu.com.cn/upload/image_hosting/8gm9vsa1.png)p + 1, newa, newb, i, maxstep, maxb);
        vnow[step] = 0;
    }
}

int main() {
    cin >> a >> b;
    
    ull g = gcd(a, b);
    a /= g;
    b /= g;
    
    for (ull maxdepth = 1; maxdepth <= 100; ++maxdepth) {
        for (ull maxb = 1000; maxb <= 10000000; maxb *= 10) {
            memset(vans, 0, sizeof(vans));
            memset(vnow, 0, sizeof(vnow));
            ansp = 0;
            
            dfs(1, a, b, 0, maxdepth, maxb);
            
            if (ansp) {
                for (ull i = 1; i <= ansp; ++i) {
                    cout << vans[i] << " ";
                }
                return 0;
            }
        }
    }
    
    return 0;
}

测试情况

2025/7/25 11:28
加载中...