zoukankan      html  css  js  c++  java
  • F-Fraction Construction Problem 2020牛客暑期多校训练营(第三场)(构造,数论,exgcd)

    F-Fraction Construction Problem 2020牛客暑期多校训练营(第三场)(构造,数论,exgcd)

    思路:

    分一下情况来讨论:

    1、(gcd(a,b)>1)

    (k=gcd(a,b)),则(frac{a+k}{b}-frac{k}{b}=frac{a/k+1}{b/k}-frac{1}{b/k}=frac{a/k}{b/k}=frac{a}{b})

    (b/k<b),所以构造上式即可满足条件。

    2、(gcd(a,b)=1),且(mathit b)至少有2个质因子。

    (b=p_1^{x_1}*p_2^{x_2}dots p_m^{x_m})(唯一分解定律),令(d=p_1^{x_1},f=b/d)

    (d*f=b)

    将题目中的公式左侧进行通分:

    (frac{c*f-e*d}{d*f}=frac{a}{b}),因为(d*f=b),所以等式转化为(c*f-e*d=a)

    又因为(gcd(f,d)=1)(原因可以看他们的构造方法,它们没有相同的质因子。)所以上面的方程一定有解(裴蜀定理 )。

    然后用(exgcd)解上面的不定方程即可。

    3、(gcd(a,b)=1,b=p^x)的情况(即b=1或者等于某个质数的幂次方)

    这种情况一定是无解的,证明如下:

    看这个方程:

    (frac{c*f-e*d}{d*f}=frac{a}{b})

    因为(gcd(a,b)=1),所以如果等式成立,要有((d*f)\%b=0)

    (d*f=p^y,f<d),则:

    (frac{c*f-e*d}{d*f}=frac{f*(c-e*d/f)}{d*f}=frac{(c-e*d/f)}{d})

    原方程转为:(frac{(c-e*d/f)}{d}=frac{a}{b})

    又因为(d<b),所以方程一定无解。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <bits/stdc++.h>
    #define ALL(x) (x).begin(), (x).end()
    #define sz(a) int(a.size())
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
    #define du2(a,b) scanf("%d %d",&(a),&(b))
    #define du1(a) scanf("%d",&(a));
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
    ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
    void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
    ' : ' ');}}
    void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
    ' : ' ');}}
    const int maxn = 2000010;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    #define DEBUG_Switch 0
    long long exgcd(long long a, long long b, long long &x, long long &y)
    {
        long long d = a;
        if (b)
        {
            d = exgcd(b, a % b, y, x);
            y -= x * (a / b);
        }
        else
        {
            x = 1;
            y = 0;
        }
        return d;
    }
     
    bool noprime[maxn + 50];
    vector <int> p;
    int getPrime()
    {
        // 华丽的初始化
        memset(noprime, false, sizeof(noprime));
        p.clear();
     
        int m = (int)sqrt(maxn + 0.5);
        // 优化的埃筛
        for (int i = 2; i <= m; i++)
        {
            if (!noprime[i])
            {
                for (int j = i * i; j <= maxn; j += i)
                {
                    noprime[j] = true;
                }
            }
        }
        // 把素数加到vector里
        for (int i = 2; i <= maxn; i++)
        {
            if (!noprime[i])
            {
                p.push_back(i);
            }
        }
        //返回vector的大小
        return p.size();
     
    }
     
    int main()
    {
    #if DEBUG_Switch
        freopen("C:\code\input.txt", "r", stdin);
    #endif
        //freopen("C:\code\output.txt","w",stdout);
        int len = getPrime();
        int t;
        t = readint();
        while (t--)
        {
            ll a = readint();
            ll b = readint();
            ll g = gcd(a, b);
            if (g != 1)
            {
                printf("%lld %lld %lld %lld
    ", a / g + 1, b / g, 1ll, b / g );
            } else
            {
                ll n = b;
                ll fac1 = 1ll;
                for (int i = 0; 1ll * p[i]*p[i] <= n && i < len; i++)
                {
                    if ( n % p[i] == 0)
                    {
                        while (n % p[i] == 0)
                        {
                            fac1 *= p[i];
                            n /= p[i];
                        }
                        break;
                    }
                }
                if (n > 1 && fac1 > 1)
                {
                    ll d = fac1, f = n;
                    ll c, e;
                    exgcd(f, d, c, e);
                    c *= a;
                    e *= a;
                    if (c > 0 && e < 0)
                    {
                        printf("%lld %lld %lld %lld
    ", c, d, -e, f );
                    } else
                    {
                        printf("%lld %lld %lld %lld
    ", e, f, -c, d );
                    }
                } else
                {
                    printf("-1 -1 -1 -1
    ");
                }
            }
        }
     
        return 0;
    }
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    海量数据面试题整理
    JConsole操作手册
    JVM调优总结(十二)-参考资料
    JVM调优总结(十一)-反思
    JVM调优总结(十)-调优方法
    JVM调优总结(八)-典型配置举例2
    JVM调优总结(九)-新一代的垃圾回收算法
    JVM调优总结(七)-典型配置举例1
    JVM调优总结(五)-分代垃圾回收详述1
    JVM调优总结(六)-分代垃圾回收详述2
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/13339228.html
Copyright © 2011-2022 走看看