SRM327 D2M IQTest

TopCoder Statistics - Problem Statement

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

数列が与えられる.必ず$a \times x[i] + b = x[i+1]$となっている.この$a, b$が複数ある場合は$AMBIGUITY$,一つもない場合は$BUG$,唯一ある場合はその数列の次の値を返す.$a$を決め打ちすると,最初の二つから$b$が決まる.後はこの法則通りに並んでいるかを確かめる.要素が1個の場合のみ,この方法では出来ないので先にはじいておいた.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define INF 1<<30
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

class IQTest {

    public:

    string nextNumber(vector <int> previous) {

      vector<ll> v(previous.begin(), previous.end());

      if(v.size() == 1) return "AMBIGUITY";

      set<ll> st;

      REP(a, -100000, 100000) {
          ll b = v[1] - (v[0] * a);
          bool ch = true;

          rep(i, v.size()-1) {
              if(v[i] * a + b == v[i+1]) continue;
              ch = false;
          }

          if(ch) {
              st.insert(v[v.size()-1] * a + b);
          }
      }

      if(st.size() == 0) return "BUG";

      if(st.size() == 1) {
          stringstream ss;
          ss << *(st.begin());
          return ss.str();
      }

      return "AMBIGUITY";
    }
};
Nov 18th, 2016