zoukankan      html  css  js  c++  java
  • 【习题 7-7 UVA-12558】Egyptian Fractions (HARD version)

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    迭代加深搜索。 枚举最大量maxdep 在dfs里面传剩余的要凑的分子、分母 以及上一次枚举的值是多少。 然后找到最小的k,满足1/k<=分子/分母 然后从max(k,last+1)开始枚举。 ->剪枝就是剩余的全都用这个最大的分数。如果都不行就肯定不行了。 二分找这个k. 不能用的数字就直接跳过就行。

    【代码】

    /*
      	1.Shoud it use long long ?
      	2.Have you ever test several sample(at least therr) yourself?
      	3.Can you promise that the solution is right? At least,the main ideal
      	4.use the puts("") or putchar() or printf and such things?
      	5.init the used array or any value?
      	6.use error MAX_VALUE?
      	7.use scanf instead of cin/cout?
      	8.whatch out the detail input require
    */
    /*
        一定在这里写完思路再敲代码!!!
    */
    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    
    const int N = 1e3;
    
    bool bo[N+10];
    int a,b,k,maxdep;
    vector<ll> v,ans;
    
    ll getidx(int a,int b){
        //1/i <= a/b 且 i最小
        //b<=a*i
        ll l= 1,r = 1e8;
        ll temp = -1;
        while (l <= r){
            int m = (l+r)>>1;
            if (1LL*a*m>=b){
                temp = m;
                r = m - 1;
            }else l = m + 1;
        }
        return temp;
    }
    
    bool Greater(vector<ll> v,vector <ll> ans){
        if ((int)ans.size()==0) return true;
        for (int i = (int)v.size()-1;i>=0;i--)
            if (v[i]!=ans[i]){
                    if (v[i]>ans[i]) return false;else return true;
                }
        return false;
    }
    
    bool dfs(int dep,int a,int b,ll last){
        if (dep==maxdep){
            if (a==1 && b>last){
                if (b<=1000 && bo[b]) return false;
                v.push_back(b);
                if (Greater(v,ans)) ans = v;
                v.pop_back();
                return true;
            }
            return false;
        }
        ll idx = getidx(a,b);
        ll ma = max(last+1,idx);
        ll delta = maxdep-dep+1;
        //delta/ma<a/b
        bool ok = false;
    
        for (ll i = ma; ;i++)
            {
                if (i<=1000 && bo[i]) continue;
                if (delta*b<a*i) break;
                //a/b - 1/i
                v.push_back(i);
                ll fenzi = a*i-b,fenmu = b*i;
                ll temp = __gcd(fenzi,fenmu);
                fenzi/=temp,fenmu/=temp;
                if (dfs(dep+1,fenzi,fenmu,i)) ok = true;
                v.pop_back();
            }
        return ok;
    }
    
    int main(){
    	#ifdef LOCAL_DEFINE
    	    freopen("rush_in.txt", "r", stdin);
    	#endif
    	ios::sync_with_stdio(0),cin.tie(0);
        int T;
        cin >> T;
        int kase = 0;
        while(T--){
            memset(bo,0,sizeof bo);
            cin >> a >> b >> k;
            while (k--){
                int x;
                cin >> x;
                bo[x] = 1;
            }
            ans.clear();
            v.clear();
            for (maxdep = 1;;maxdep++){
                if (dfs(1,a,b,1)){
                    cout <<"Case "<<++kase<<": "<<a<<"/"<<b<<"=1/"<<ans[0];
                    for (int i = 1;i <(int) ans.size();i++)
                        cout << "+1/"<<ans[i];
                    break;
                }
            }
            cout << endl;
        }
    	return 0;
    }
    
    
  • 相关阅读:
    web 安全问题(二):XSS攻击
    web 安全问题(一):CSRF 攻击
    关于阅读源码
    vue 阅读一【待完结】
    【转】服务器添加新用户用ssh-key 登录,并禁用root用户 密码登录
    sass & compass 实战录
    浏览器兼容性总结
    常用的一个cookie 对象,还有path 兼容性问题
    css margin塌陷问题
    markdown 语法简要备忘
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8159189.html
Copyright © 2011-2022 走看看