Problem:
D : Chopsticks / 箸
Problem
お箸は 2 本 1 セット(1 膳)で用いるものである。しかしあるとき一人暮らしの大学生であるいあづ君は箸立てに入っているお箸が奇数本であることに気がついた。これは明らかにおかしい。いつの間にか何本か無くしてしまったのである。
困ったいあづ君は、今後このようなことが無いように箸の本数を毎日記録することにした。
ところが、いあづ君が決めた記録の方法は少し変わっている。 それは、まず
M
個の自然数
A
1
, ... ,
A
M
を決めた後、 箸の本数をそれらで割った余りを毎日記録するというものである。 例えばある日に残っている箸の本数が
T
のとき、 その日の記録として
T
%
A
1
, ... ,
T
%
A
M
を付ける。 ただ、たまにいくつか記録し忘れたり、間違った記録を付けたりすることもあるようだ。 (
x
%
y
は
x
を
y
で割った余りである。)
ここでいあづ君は大事なことに気づいた。 これだと最終日に何本残っているか 1 つに決まらない！
ポジティブないあづ君は、
D
日分の記録をもとに、
D
日目の記録をつけた直後に残っている箸の本数の最大値を求めるプログラムを書くことにした。
Input
入力は次のような形式で与えられる．
N
M
D
A
1
A
2
...
A
M
R
11
R
12
...
R
1M
R
21
R
22
...
R
2M
...
R
D1
R
12
...
R
DM
N
,
M
,
D
は、それぞれ記録を開始する直前にあったお箸の本数、余りを求めるのに使う整数の個数、記録をつけた日数である。
A
1
A
2
...
A
M
は余りを求めるのに使う整数である。
2 +
d
行目の
R
d1
,
R
d2
, ... ,
R
dM
は、
d
日目に残っているお箸の本数を
A
1
,
A
2
, ... ,
A
M
のそれぞれで割った余りである。
R
di
= -1 のときは、その記録を付け忘れたことを意味する。
Constraints
入力は全て整数である。
1 ≦
N
≦ 1,000,000,000
1 ≦
M
≦ 10
1 ≦
D
≦ 100
2 ≦
A
i
≦ 100
i
≠
j
のとき
A
i
≠
A
j
0 ≦
R
ij
<
A
i
i
日目に存在する箸の数は、
i
- 1 日目に存在していた箸の数と同等か、それより少なくなる。
Output
D
日目に残っていると考えられる箸の数の最大値を 1 行で出力せよ。 記録に明らかに間違いがある場合は -1 を出力せよ。
Samples
Sample Input 1
3 3 2
2 3 4
1 0 3
0 2 2
Sample Output 1
2
0 日目に 3 本あった箸は、 1 日目に 3 本、 2 日目に 2 本になったと考えられる。
Sample Input 2
3 3 2
2 3 4
0 0 0
0 0 1
Sample Output 2
-1
0 日目に 3 本あった箸は、 1 日目に 0 本になったと考えられる。ところが 2 日目の 4 で割った余りが1になっている。
Sample Input 3
3 3 2
2 3 4
1 0 3
-1 -1 -1
Sample Output 3
3
0 日目に 3 本あった箸は、 1 日目も 3 本あったと考えられる。2 日目は記録し忘れてしまった。

Submitted solutions:

code_snippet: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

const int LG10_BASE = 9, BASE = 1000000000; // BASE = 10^{LG_10BASE}
struct BigInt {
  int sign; vector<int> dat;
  BigInt(ll val = 0) { *this = val; }
  BigInt(const string &s) { *this = s; }
  vector<ll> convert_base(int new_lg10_base, int new_base) const {
    // assert(new_base == static_cast<int>(round(pow(10, new_lg10_base))));
    int mx_base = max(LG10_BASE, new_lg10_base);
    vector<ll> p(mx_base + 1, 1);
    FOR(i, 1, mx_base + 1) p[i] = p[i - 1] * 10;
    vector<ll> res;
    ll now_val = 0;
    int now_lg10_base = 0;
    for (int e : dat) {
      now_val += p[now_lg10_base] * e;
      now_lg10_base += LG10_BASE;
      while (now_lg10_base >= new_lg10_base) {
        res.emplace_back(now_val % new_base);
        now_val /= new_base;
        now_lg10_base -= new_lg10_base;
      }
    }
    res.emplace_back(now_val);
    while (!res.empty() && res.back() == 0) res.pop_back();
    return res;
  }
  int digit_sum() {
    assert(sign == 1);
    string s = to_string();
    int res = 0;
    for (char c : s) res += c - '0';
    return res;
  }
  int length() {
    if (dat.empty()) return 0;
    int res = LG10_BASE * (dat.size() - 1);
    int tmp = dat.back();
    while (tmp > 0) { ++res; tmp /= 10; }
    return res;
  }
  BigInt pow(BigInt exponent) {
    BigInt res = 1, tmp = *this;
    while (exponent > 0) {
      if (exponent.dat[0] & 1) res *= tmp;
      tmp *= tmp;
      exponent /= 2;
    }
    return res;
  }
  ll to_llong() const {
    assert(*this >= LLONG_MIN && *this <= LLONG_MAX);
    ll res = 0;
    for (int i = static_cast<int>(dat.size()) - 1; i >= 0; --i) (res *= BASE) += dat[i];
    return res;
  }
  string to_string() { stringstream ss; ss << *this; string res; ss >> res; return res; }
  void trim() {
    while (!dat.empty() && dat.back() == 0) dat.pop_back();
    if (dat.empty()) sign = 1;
  }
  BigInt &operator=(ll val) {
    sign = 1;
    if (val < 0) { sign = -1; val = -val;}
    dat.clear();
    for (; val > 0; val /= BASE) dat.emplace_back(val % BASE);
    return *this;
  }
  BigInt &operator=(const string &s) {
    sign = 1;
    dat.clear();
    if (!s.empty()) {
      int tail = 0;
      if (s[tail] == '-' || s[tail] == '+') {
        if (s[tail] == '-') sign = -1;
        ++tail;
      }
      for (int i = s.length() - 1; i >= tail; i -= LG10_BASE) {
        int val = 0;
        for (int j = max(tail, i - LG10_BASE + 1); j <= i; ++j) val = val * 10 + (s[j] - '0');
        dat.emplace_back(val);
      }
    }
    trim();
    return *this;
  }
  BigInt &operator=(const BigInt &x) {
    sign = x.sign;
    dat.resize(x.dat.size());
    copy(ALL(x.dat), dat.begin());
    return *this;
  }
  BigInt &operator+=(const BigInt &x) {
    if (sign == x.sign) {
      bool carry = false;
      for (int i = 0; i < max(dat.size(), x.dat.size()) || carry; ++i) {
        if (i == dat.size()) dat.emplace_back(0);
        dat[i] += (i < x.dat.size() ? x.dat[i] : 0) + carry;
        carry = dat[i] >= BASE;
        if (carry) dat[i] -= BASE;
      }
    } else {
      *this -= -x;
    }
    return *this;
  }
  BigInt &operator-=(const BigInt &x) {
    if (sign == x.sign) {
      BigInt abs_this = *this, abs_x = x;
      abs_this.sign = 1; abs_x.sign = 1;
      if (abs_this >= abs_x) {
        bool carry = false;
        for (int i = 0; i < dat.size() || carry; ++i) {
          dat[i] -= (i < x.dat.size() ? x.dat[i] : 0) + carry;
          carry = dat[i] < 0;
          if (carry) dat[i] += BASE;
        }
        trim();
      } else {
        *this = -(x - *this);
      }
    } else {
      *this += -x;
    }
    return *this;
  }
  BigInt &operator*=(const BigInt &x) {
    const int new_log10_base = 6, new_base = 1000000;
    vector<ll> this6 = convert_base(new_log10_base, new_base), x6 = x.convert_base(new_log10_base, new_base);
    vector<ll> res = karatsuba(this6, 0, this6.size(), x6, 0, x6.size());
    REP(i, res.size()) {
      ll quo = res[i] / new_base;
      if (quo > 0) {
        if (i + 1 == res.size()) res.emplace_back(0);
        res[i + 1] += quo;
      }
      res[i] %= new_base;
    }
    string s = (sign * x.sign == 1 ? "+" : "-");
    for (int i = static_cast<int>(res.size()) - 1; i >= 0; --i) {
      string tmp = std::to_string(res[i]);
      REP(_, new_log10_base - tmp.size()) s += '0';
      s += tmp;
    }
    return *this = s;
  }
  BigInt &operator/=(int x) { return *this = divide(x).first; }
  BigInt &operator/=(const BigInt &x) { return *this = divide(x).first; }
  BigInt &operator%=(int x) { return *this = divide(x).second; }
  BigInt &operator%=(const BigInt &x) { return *this = divide(x).second; }
  bool operator==(const BigInt &x) const {
    if (sign != x.sign || dat.size() != x.dat.size()) return false;
    int sz = dat.size();
    REP(i, sz) if (dat[i] != x.dat[i]) return false;
    return true;
  }
  bool operator!=(const BigInt &x) const { return !(*this == x); }
  bool operator<(const BigInt &x) const {
    if (sign != x.sign) return sign < x.sign;
    if (dat.size() != x.dat.size()) return sign * dat.size() < x.sign * x.dat.size();
    for (int i = static_cast<int>(dat.size()) - 1; i >= 0; --i) {
      if (dat[i] != x.dat[i]) return dat[i] * sign < x.dat[i] * x.sign;
    }
    return false;
  }
  bool operator<=(const BigInt &x) const { return !(x < *this); }
  bool operator>(const BigInt &x) const { return x < *this; }
  bool operator>=(const BigInt &x) const { return !(*this < x); }
  BigInt &operator++() { return *this += 1; }
  BigInt operator++(int) { BigInt res = *this; ++*this; return res; }
  BigInt &operator--() { return *this -= 1; }
  BigInt operator--(int) { BigInt res = *this; --*this; return res; }
  BigInt operator+() const { return *this; }
  BigInt operator-() const { BigInt res = *this; res.sign = -res.sign; return res; }
  BigInt operator+(const BigInt &x) const { return BigInt(*this) += x; }
  BigInt operator-(const BigInt &x) const { return BigInt(*this) -= x; }
  BigInt operator*(const BigInt &x) const { return BigInt(*this) *= x; }
  BigInt operator/(int x) const { return BigInt(*this) /= x; }
  BigInt operator/(const BigInt &x) const { return BigInt(*this) /= x; }
  BigInt operator%(int x) const { return BigInt(*this) %= x; }
  BigInt operator%(const BigInt &x) const { return BigInt(*this) %= x; }
  friend ostream &operator<<(ostream &os, const BigInt &x) {
    if (x.sign == -1) os << '-';
    os << (x.dat.empty() ? 0 : x.dat.back());
    for (int i = static_cast<int>(x.dat.size()) - 2; i >= 0; --i) os << setw(LG10_BASE) << setfill('0') << x.dat[i];
    return os;
  }
  friend istream &operator>>(istream &is, BigInt &x) { string s; is >> s; x = s; return is; }
private:
  vector<ll> karatsuba(vector<ll> &a, int a_l, int a_r, vector<ll> &b, int b_l, int b_r) {
    int a_len = a_r - a_l, b_len = b_r - b_l;
    if (a_len < b_len) return karatsuba(b, b_l, b_r, a, a_l, a_r);
    vector<ll> res(a_len + b_len, 0);
    if (b_len <= 32) {
      FOR(i, a_l, a_r) FOR(j, b_l, b_r) res[(i - a_l) + (j - b_l)] += a[i] * b[j];
    } else {
      int mid = (a_len + 1) / 2, n = min(a_len, mid);
      for (int i = a_l; i + mid < a_r; ++i) a[i] += a[i + mid];
      for (int i = b_l; i + mid < b_r; ++i) b[i] += b[i + mid];
      vector<ll> tmp = karatsuba(a, a_l, a_l + mid, b, b_l, b_l + n);
      REP(i, tmp.size()) res[mid + i] = tmp[i];
      for (int i = a_l; i + mid < a_r; ++i) a[i] -= a[i + mid];
      for (int i = b_l; i + mid < b_r; ++i) b[i] -= b[i + mid];
      tmp = karatsuba(a, a_l, a_l + mid, b, b_l, b_l + n);
      REP(i, tmp.size()) { res[i] += tmp[i]; res[mid + i] -= tmp[i]; }
      tmp = karatsuba(a, a_l + mid, a_r, b, b_l + n, b_r);
      REP(i, tmp.size()) { res[mid + i] -= tmp[i]; res[(mid << 1) + i] += tmp[i]; }
    }
    while (!res.empty() && res.back() == 0) res.pop_back();
    return res;
  }
  pair<BigInt, int> divide(int x) {
    // assert(!x.dat.empty());
    BigInt dividend = *this;
    if (x < 0) { dividend.sign = -dividend.sign; x = -x; }
    ll rem = 0;
    for (int i = static_cast<int>(dividend.dat.size()) - 1; i >= 0; --i) {
      ll tmp = rem * BASE + dividend.dat[i];
      dividend.dat[i] = static_cast<int>(tmp / x);
      rem = tmp % x;
    }
    dividend.trim();
    return {dividend, static_cast<int>(rem)};
  }
  pair<BigInt, BigInt> divide(const BigInt &x) {
    // assert(!x.dat.empty());
    int k = BASE / (x.dat.back() + 1);
    BigInt dividend = *this, divisor = x;
    dividend.sign = 1; divisor.sign = 1;
    dividend *= k; divisor *= k;
    BigInt quo, rem = 0;
    quo.dat.resize(dividend.dat.size());
    int sz = divisor.dat.size();
    for (int i = static_cast<int>(dividend.dat.size()) - 1; i >= 0; --i) {
      rem.dat.insert(rem.dat.begin(), dividend.dat[i]);
      quo.dat[i] = static_cast<int>(((sz < rem.dat.size() ? 1LL * rem.dat[sz] * BASE : 0) + (sz - 1 < rem.dat.size() ? rem.dat[sz - 1] : 0)) / divisor.dat.back());
      rem -= divisor * quo.dat[i];
      while (rem.sign == -1) { rem += divisor; --quo.dat[i];  }
    }
    quo.sign = sign * x.sign; rem.sign = sign;
    quo.trim(); rem.trim();
    return {quo, rem / k};
  }
};
BigInt __gcd(BigInt a, BigInt b) { while (!b.dat.empty()) swap(a %= b, b); return a; }
BigInt __lcm(const BigInt &a, const BigInt &b) { return a / __gcd(a, b) * b; }
BigInt abs(const BigInt &x) { BigInt res = x; res.sign = 1; return res; }
BigInt max(const BigInt &a, const BigInt &b) { return a < b ? b : a; }
BigInt min(const BigInt &a, const BigInt &b) { return a < b ? a : b; }

template <typename T>
pair<T, T> ext_gcd(T a, T b) {
  if (b == 0) return {1, 0};
  T fst, snd; tie(fst, snd) = ext_gcd(b, a % b);
  return {snd, fst - a / b * snd};
}

template <typename T>
pair<T, T> crt(const vector<T> &b, const vector<T> &m) {
  T x = 0, md = 1;
  int n = b.size();
  REP(i, n) {
    T g = __gcd(md, m[i]);
    if ((b[i] - x) % g != 0) return {0, -1};
    x += md * ((b[i] - x) / g * ext_gcd(md, m[i]).first % (m[i] / g));
    md *= m[i] / g;
  }
  return {x < 0 ? x + md : x, md};
}

int main() {
  BigInt n; int m, d; cin >> n >> m >> d;
  vector<BigInt> a(m); REP(i, m) cin >> a[i];
  while (d--) {
    vector<BigInt> r, A;
    REP(i, m) {
      int ri; cin >> ri;
      if (ri != -1) {
        r.emplace_back(ri);
        A.emplace_back(a[i]);
      }
    }
    BigInt x, y; tie(x, y) = crt(r, A);
    if (y == -1 || x > n) {
      cout << "-1\n";
      return 0;
    }
    n = (n - x) / y * y + x;
  }
  cout << n << '\n';
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3352
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;

int cal(int n,P p){
  while(n%p.first!=p.second&&n>=0)n--;
  return n;
}

int main(){
  int n,m,d,a[10];
  cin>>n>>m>>d;
  for(int i=0;i<m;i++){
    cin>>a[i];
  }
  P r[100][10];
  for(int i=0;i<d;i++){
    for(int j=0;j<m;j++){
      cin>>r[i][j].second;
      r[i][j].first=a[j];
    }
    sort(r[i],r[i]+m);
    reverse(r[i],r[i]+m);
    vector<int> v;
    int u=1e6;
    for(int j=0;j<m;j++){
      if(r[i][j].second==-1)continue;
      if(j){
	vector<int> nv;
	for(int k=0;k<(int)v.size();k++){
	  if(v[k]%r[i][j].first==r[i][j].second)nv.push_back(v[k]);
	}
	v=nv;
      }
      else {
	int p=cal(n,r[i][j]);
	while((int)v.size()<u&&p>=0){
	  v.push_back(p);
	  p-=r[i][j].first;
	}
      }
    }
    if(v.size())n=v[0];
    else if(r[i][0].second!=-1)n=-1;
  }
  cout<<n<<endl;
  return 0;
}
accuracy: 0.3275862068965517
time_ms: 560
memory_kb: 10896
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N,M,D;
ll A[10];
ll INF = 1e18;

ll lcm(ll a,ll b){
  ll res= a/__gcd(a,b);
  if( res > INF / b )return INF;
  return res * b;
}

ll solve(){

  ll ans=N;
  for(int i=0;i<D;i++){
    ll a=0,b=1;
    for(int j=0;j<M;j++){
      ll k;
      cin>>k;
      if(k==-1)continue;
      if(k>=A[j])return -1;

      if(a==0 && b==1){
        a=k;
        b=A[j];
        continue;
      }

      while( a%A[j]!=k ){
        if(a+b > N)return -1;
        a+=b;
      }
      b=lcm(b,A[j]);
    }

    
    if(ans<a)return -1;
    ll d=ans-a;
    ll c=d/b;
    ans=a+b*c;
  }
  return ans;
}

int main(){
  cin>>N>>M>>D;
  for(int i=0;i<M;i++)cin>>A[i];
  cout<<solve()<<endl;
  return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 1160
final_rank: 4

code_snippet: #include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N, M, D;
    vector<int> A;
    vector< vector<int> > R;
    void input() {
        cin >> N >> M >> D;
        A.resize(M); cin >> A;
        R.resize(D);
        for (int i = 0; i < D; i++) {
            R[i].resize(M); cin >> R[i];
        }
    }

    ll lcm(ll a, ll b) {
        return a / __gcd(a, b) * b;
    }

    bool check(int n, int d) {
        for (int i = 0; i < M; i++) {
            if (R[d][i] < 0) continue;
            if (n % A[i] != R[d][i]) return false;
        }
        return true;
    }

    void solve() {

        ll x = N;
        for (int d = 0; d < D; d++) {
            int a = 0;
            int m = -1;
            ll L = 1;
            for (int i = 0; i < M; i++) {
                if (R[d][i] < 0) continue;
                L = lcm(L, A[i]);
                if (a < A[i]) {
                    a = A[i];
                    m = i;
                }
            }
            if (m < 0) continue;
            int n = R[d][m];
            for (; ; n += A[m]) {
                if (n > x) {
                    cout << -1 << endl;
                    return;
                }
                if (check(n, d)) {
                    break;
                }
            }
            x = (x - n) / L * L + n;
        }
        cout << x << endl;

    }
}

int main() {
    input(); solve();
    return 0;
}
accuracy: 1
time_ms: 900
memory_kb: 3104
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N, M, D;
    vector<int> A;
    vector< vector<int> > R;
    void input() {
        cin >> N >> M >> D;
        A.resize(M); cin >> A;
        R.resize(D);
        for (int i = 0; i < D; i++) {
            R[i].resize(M); cin >> R[i];
        }
    }

    ll lcm(ll a, ll b) {
        return a / __gcd(a, b) * b;
    }

    bool check(int n, int d) {
        for (int i = 0; i < M; i++) {
            if (R[d][i] < 0) continue;
            if (n % A[i] != R[d][i]) return false;
        }
        return true;
    }

    void solve() {
        ll L = 1;
        for (int i = 0; i < M; i++) L = lcm(L, A[i]);

        ll x = N;
        for (int d = 0; d < D; d++) {
            int a = 0;
            int m = -1;
            for (int i = 0; i < M; i++) {
                if (R[d][i] < 0) continue;
                if (a < A[i]) {
                    a = A[i];
                    m = i;
                }
            }
            if (m < 0) continue;
            int n = R[d][m];
            for (; ; n += A[m]) {
                if (n > x) {
                    cout << -1 << endl;
                    return;
                }
                if (check(n, d)) {
                    break;
                }
            }
            x = (x - n) / L * L + n;
        }
        cout << x << endl;

    }
}

int main() {
    input(); solve();
    return 0;
}
accuracy: 0.3103448275862069
time_ms: 900
memory_kb: 3104
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N, M, D;
    vector<int> A;
    vector< vector<int> > R;
    void input() {
        cin >> N >> M >> D;
        A.resize(M); cin >> A;
        R.resize(D);
        for (int i = 0; i < D; i++) {
            R[i].resize(M); cin >> R[i];
        }
    }

    ll lcm(ll a, ll b) {
        return a / __gcd(a, b) * b;
    }

    bool check(int n, int d) {
        for (int i = 0; i < M; i++) {
            if (R[d][i] < 0) continue;
            if (n % A[i] != R[d][i]) return false;
        }
        return true;
    }

    void solve() {
        ll L = 1;
        for (int i = 0; i < M; i++) L = lcm(L, A[i]);

        ll x = N;
        for (int d = 0; d < D; d++) {
            int m = 0;
            for (int i = 0; i < M; i++) {
                if (R[d][i] < 0) continue;
                if (A[m] < A[i]) {
                    m = i;
                }
            }
            int n = R[d][m];
            if (n < 0) continue;
            for (; ; n += A[m]) {
                if (n > x) {
                    cout << -1 << endl;
                    return;
                }
                if (check(n, d)) {
                    break;
                }
            }
            x = (x - n) / L * L + n;
        }
        cout << x << endl;

    }
}

int main() {
    input(); solve();
    return 0;
}
accuracy: 0.3103448275862069
time_ms: 920
memory_kb: 3104
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N, M, D;
    vector<int> A;
    vector< vector<int> > R;
    void input() {
        cin >> N >> M >> D;
        A.resize(M); cin >> A;
        R.resize(D);
        for (int i = 0; i < D; i++) {
            R[i].resize(M); cin >> R[i];
        }
    }

    ll lcm(ll a, ll b) {
        return a / __gcd(a, b) * b;
    }

    bool check(int n, int d) {
        for (int i = 0; i < M; i++) {
            if (R[d][i] < 0) continue;
            if (n % A[i] != R[d][i]) return false;
        }
        return true;
    }

    void solve() {
        ll L = 1;
        for (int i = 0; i < M; i++) L = lcm(L, A[i]);

        ll x = N;
        for (int d = 0; d < D; d++) {
            int m = 0;
            for (int i = 0; i < M; i++) {
                if (A[m] < A[i]) {
                    m = i;
                }
            }
            int n = R[d][m];
            if (n < 0) continue;
            for (; ; n += A[m]) {
                if (n > x) {
                    cout << -1 << endl;
                    return;
                }
                if (check(n, d)) {
                    break;
                }
            }
            x = (x - n) / L * L + n;
        }
        cout << x << endl;

    }
}

int main() {
    input(); solve();
    return 0;
}
accuracy: 0.3103448275862069
time_ms: 900
memory_kb: 3104
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N, M, D;
    vector<int> A;
    vector< vector<int> > R;
    void input() {
        cin >> N >> M >> D;
        A.resize(M); cin >> A;
        R.resize(D);
        for (int i = 0; i < D; i++) {
            R[i].resize(M); cin >> R[i];
        }
    }

    ll lcm(ll a, ll b) {
        return a / __gcd(a, b) * b;
    }

    bool check(int n, int d) {
        for (int i = 0; i < M; i++) {
            if (n % A[i] != R[d][i]) return false;
        }
        return true;
    }

    void solve() {
        ll L = 1;
        for (int i = 0; i < M; i++) L = lcm(L, A[i]);

        int m = 0;
        for (int i = 0; i < M; i++) {
            if (A[m] < A[i]) {
                m = i;
            }
        }

        ll x = N;
        for (int d = 0; d < D; d++) {
            int n = R[d][m];
            if (n < 0) continue;
            for (; ; n += A[m]) {
                if (n > x) {
                    cout << -1 << endl;
                    return;
                }
                if (check(n, d)) {
                    break;
                }
            }
            x = (x - n) / L * L + n;
        }
        cout << x << endl;

    }
}

int main() {
    input(); solve();
    return 0;
}
accuracy: 0.3103448275862069
time_ms: 880
memory_kb: 3108
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N, M, D;
    vector<int> A;
    vector< vector<int> > R;
    void input() {
        cin >> N >> M >> D;
        A.resize(M); cin >> A;
        R.resize(D);
        for (int i = 0; i < D; i++) {
            R[i].resize(M); cin >> R[i];
        }
    }

    int lcm(int a, int b) {
        return a / __gcd(a, b) * b;
    }

    bool check(int n, int d) {
        for (int i = 0; i < M; i++) {
            if (n % A[i] != R[d][i]) return false;
        }
        return true;
    }

    void solve() {
        int L = 1;
        for (int i = 0; i < M; i++) L = lcm(L, A[i]);

        int m = 0;
        for (int i = 0; i < M; i++) {
            if (A[m] < A[i]) {
                m = i;
            }
        }

        int x = N;
        for (int d = 0; d < D; d++) {
            int n = R[d][m];
            if (n < 0) continue;
            for (; ; n += A[m]) {
                if (n > x) {
                    cout << -1 << endl;
                    return;
                }
                if (check(n, d)) {
                    break;
                }
            }
            x = (x - n) / L * L + n;
        }
        cout << x << endl;

    }
}

int main() {
    input(); solve();
    return 0;
}
accuracy: 0.3103448275862069
time_ms: 820
memory_kb: 3100
final_rank: 7

code_snippet: #include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<cstring>
#include<sstream>
#include<complex>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < int(Y);++(X))
#define rrep(X,Y) for (int (X) = int(Y-1);(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef ll Int;
ll INF=1ll<<45;
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

int main(){
  int i,j,k;
  ll n,m,d;
  cin>>n>>m>>d;
  vector<ll> a(m),r(m),aa(m),gcda(m);
  vector<vector<ll > > subs(m);
  rep(i,m)
    cin>>a[i];
  aa[0]=1;
  rep(i,m-1)
    aa[i+1]=aa[i]*a[i]/__gcd(aa[i],a[i]);
  rep(i,m)
    gcda[i]=__gcd(aa[i],a[i]);
  rep(i,m){
    subs[i].resize(a[i],INF);
    rep(j,aa[i]+1){
      ll &tmp=subs[i][j*aa[i]%a[i]];
      tmp=min(tmp,j*aa[i]);
    }
//rep(j,a[i])cout<<subs[i][j]<<",";cout<<endl;
  }
  ll dd;
  rep(i,d){
    rep(j,m)
      cin>>r[j];
    rep(j,m){
      if(r[j]<0)continue;
      if((n+a[j]-r[j])%a[j]){
        ll nn=(n+a[j]-r[j])%a[j];
        if(nn%gcda[j] || subs[j][nn]==INF){
          cout<<-1<<endl;
          return 0;
        }
        n-=subs[j][nn];
        if(n<0){
          cout<<-1<<endl;
          return 0;
        }
      }
 //     cout<<"->"<<n<<":"<<r[j]<<"mod"<<a[j]<<endl;
    }
  }
  cout<<n<<endl;
  return 0;
}
accuracy: 0.05172413793103448
time_ms: 10
memory_kb: 1200
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
typedef vector<pr > vpr;
const int INF=1<<30;
ll lcm(ll a,ll b){
	if(a%b==0)
	return b;
	else
	return lcm(b,a%b);
}
ll GCD(ll a,ll b){
	return (a*b)/lcm(a,b);
}
int main() {
	ll N,M,D;
	ll divi[10];
	ll dived[100][10],gcd[10]={};
	cin >> N >> M >> D;
	REP(i,M){
	cin >> divi[i];
	}
	REP(i,D)
		REP(j,M)
			cin >> dived[i][j];
	ll now=N;
	REP(i,D){
		ll mn=0,ngcd=1;
		bool ans=false;
		REP(j,M){
			if(dived[i][j]==-1)	
				continue;
			if(!ans)
				mn=dived[i][j];
			ans=true;
			while(mn%divi[j]!=dived[i][j] && now>=mn) mn+=ngcd;
			ngcd=GCD(ngcd,divi[j]);
		}
		if(now<mn){
			now=-1;
			break;
		}
		if(ans){
			ll alpha=(now-mn)/ngcd;
			now=mn+alpha*ngcd;
		}
	}
	cout << now << endl;
	// your code goes here
	return 0;
}
accuracy: 1
time_ms: 500
memory_kb: 1164
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
using namespace std;
typedef pair<int,int> pr;
typedef vector<pr > vpr;
const int INF=1<<30;
int lcm(int a,int b){
	if(a%b==0)
	return b;
	else
	return lcm(b,a%b);
}
int GCD(int a,int b){
	return (a*b)/lcm(a,b);
}
int main() {
	int N,M,D;
	int divi[10];
	int dived[100][10],gcd[10]={};
	cin >> N >> M >> D;
	REP(i,M){
	cin >> divi[i];
	if(i)
	gcd[i]=GCD(gcd[i-1],divi[i]);
	else
	gcd[i]=divi[i];
	}
	REP(i,D)
		REP(j,M)
			cin >> dived[i][j];
	int now=N;
	REP(i,D){
		int mn=0;
		bool ans=false;
		REP(j,M){
		if(dived[i][j]==-1)	
		continue;
		ans=true;
		if(mn==0)
			mn=dived[i][j];
		while(mn%divi[j]!=dived[i][j] && N>mn) mn+=gcd[i];
		}
		if(now<mn){
			now=-1;
			break;
		}
		if(ans){
		int alpha=(now-mn)/gcd[M-1];
		now=mn+alpha*gcd[M-1];
		}
	}
	cout << now << endl;
	// your code goes here
	return 0;
}
accuracy: 0.05172413793103448
time_ms: 20
memory_kb: 1164
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() {return val;}
    bool operator!=(Iterator &itr) {return val < itr.val;}
    void operator++() {++val;}
  };
  Iterator i, n;
public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator &begin() {return i;}
  Iterator &end() {return n;}
};

template<class T> T at(vector<T> v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];}

long long mod_inverse(long long a, long long mod) {
  long long b = mod, x = 1, y = 0;
  while (b) {
    long long t = a / b;
    swap(a -= t * b, b);
    swap(x -= t * y, y);
  }
  return (mod + x % mod) % mod;
}

pair<long long, long long> linear_congruence(const vector<long long>& A, const vector<long long>& B, const vector<long long>& M) {
  long long x = 0, m = 1;
  for (int i : range(A.size())) {
    long long a = A[i] * m;
    long long b = B[i] - A[i] * x;
    long long d = __gcd(M[i], a);
    if (b % d != 0) return make_pair(0, -1);
    int t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
    x = x + m * t;
    m *= M[i] / d;
  }
  return make_pair(x % m, m);
}

const long long INF = 1e9;

int main() {
  int n, m, d;
  cin >> n >> m >> d;
  long long a[m];
  for (int i : range(m)) cin >> a[i];
  long long r[d][m];
  for (int i : range(d)) for (int j : range(m)) cin >> r[i][j];
  vector<long long> A, B, M;
  for (int i : range(d)) {
    A.clear(); B.clear(); M.clear();
    for (int j : range(m)) if (r[i][j] != -1) {
      A.emplace_back(1);
      B.emplace_back(r[i][j]);
      M.emplace_back(a[j]);
    }
    if (A.empty()) continue;
    auto x = linear_congruence(A, B, M);
    if (x.second == -1) {
      cout << -1 << endl;
      return 0;
    }
    while (x.first + x.second <= n) x.first += x.second;
    if (x.first > n) {
      cout << -1 << endl;
      return 0;
    }
    n = x.first;
  }
  cout << n << endl;
}
accuracy: 1
time_ms: 180
memory_kb: 1228
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() {return val;}
    bool operator!=(Iterator &itr) {return val < itr.val;}
    void operator++() {++val;}
  };
  Iterator i, n;
public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator &begin() {return i;}
  Iterator &end() {return n;}
};

template<class T> T at(vector<T> v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];}

long long mod_inverse(long long a, long long mod) {
  long long b = mod, x = 1, y = 0;
  while (b) {
    long long t = a / b;
    swap(a -= t * b, b);
    swap(x -= t * y, y);
  }
  return (mod + x % mod) % mod;
}

pair<long long, long long> linear_congruence(const vector<long long>& A, const vector<long long>& B, const vector<long long>& M) {
  long long x = 0, m = 1;
  for (int i : range(A.size())) {
    long long a = A[i] * m;
    long long b = B[i] - A[i] * x;
    long long d = __gcd(M[i], a);
    if (b % d != 0) return make_pair(0, -1);
    int t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
    x = x + m * t;
    m *= M[i] / d;
  }
  return make_pair(x % m, m);
}

const long long INF = 1e9;

int main() {
  int n, m, d;
  cin >> n >> m >> d;
  long long a[m];
  for (int i : range(m)) cin >> a[i];
  long long r[d][m];
  for (int i : range(d)) for (int j : range(m)) cin >> r[i][j];
  vector<long long> A, B, M;
  for (int i : range(d)) {
    A.clear(); B.clear(); M.clear();
    for (int j : range(m)) if (r[i][j] != -1) {
      A.emplace_back(1);
      B.emplace_back(r[i][j]);
      M.emplace_back(a[j]);
    }
    if (A.empty()) continue;
    auto x = linear_congruence(A, B, M);
    if (x.second == -1) {
      cout << -1 << endl;
      return 0;
    }
    while (x.first + x.second < n) x.first += x.second;
    if (x.first > n) {
      cout << -1 << endl;
      return 0;
    }
    n = x.first;
  }
  cout << n << endl;
}
accuracy: 0.13793103448275862
time_ms: 30
memory_kb: 1228
final_rank: 12