Topcoder SRM 485 Div1 250

题意:有一个数列A,A是等差数列,至少含有4个元素,小明将它的每个偶数都除2除2..直到不是偶数为止,这样A就变成了数列B。现在给的是数列B,要求恢复数列A,所有解中求字典序最小解!

解法:枚举第一个数,再枚举第二个数,再O(N)判断后面的数是否可以成立。。即可,,最后一步每个点的判断可以达到O(1)级别。。

Code

#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
class AfraidOfEven{
public:
    ll limit = (1LL << 31) - 1;
    vector<int>getans(int ty, vector<int>&seq){
        bool ok1 = 0;
        ll s0 = 0, s1 = 0;
        for(ll i0 = seq[0]; i0 <= limit && ok1 == 0; i0 += i0){
            for(ll i1 = seq[1]; i1 <= limit && ok1 == 0; i1 += i1){
                if(ty == 1 && i1 < i0) continue;
                else if(ty == 2 && i1 > i0) break;
                ll d = i1 - i0;
                ll sho = i1 + d;
                bool ok = 1;
                for(int j = 2; j < seq.size() && ok == 1; j++){
                    if(sho % seq[j] == 0){
                        ll t = sho / seq[j];
                        if((t | (t&-t)) != (t&-t)) ok = 0;
                    }
                    else ok = 0;
                    sho += d;
                }
                if(ok)
                {
                    ok1 = 1;
                    s0 = i0;
                    s1 = i1;
                }
            }
        }
        vector<int>ans; ans.clear();
        if(ok1){
            ans.push_back((int)s0);
            ans.push_back((int)s1);
            while(ans.size() < seq.size()){
                int t = 2 * ans.back() - ans[(int)ans.size() - 2];
                ans.push_back(t);
            }
        }
        return ans;
    }
    vector <int> restoreProgression(vector <int> seq){
        vector<int>ans1 = getans(1, seq);
        vector<int>ans2 = getans(2, seq);
        if(ans1.size() == 0) return ans2;
        else if(ans2.size() == 0) return ans1;
        else{
            bool y = 1;
            for(int i = 0; i < ans1.size() && y == 1; i++){
                if(ans1[i] < ans2[i]) break;
                else if(ans1[i] > ans2[i]) y = 0;
            }
            if(y == 1) return ans1;
            else return ans2;
        }
    }
};

你可能感兴趣的:(Topcoder SRM 485 Div1 250)