AtCoder M-SOLUTIONS 2019 Task E. Product of Arithmetic Progression

problem link

Official editorial:

code:

int main() {
#if defined LOCAL && !defined DUIPAI
  ifstream in("main.in");
  cin.rdbuf(in.rdbuf());
//  ofstream out("main.out");
//  cout.rdbuf(out.rdbuf());
#endif
  vector<Mint> fact(md);
  fact[0] = 1;
  for (int i = 1; i < md; ++i) {
    fact[i] = i * fact[i - 1];
  }
int Q;
scan(Q);
//1000003是素数
rep (Q) {
  int x, d, n;
  scan(x, d, n);
  // 两种特殊情况:1.d==0,2.数列中出现了0
  if (d == 0) {
    println(power(Mint(x), n));
    continue;
  }
  if (n >= md || x == 0) {
    println(0);
    continue;
  }
  // 在域上是可以做除法的
  auto inv_d = inverse(d, md);
  if (inv_d < 0) inv_d += md;
  int _x = (ll) x * inv_d % md;
  if (_x + n - 1 >= md) {
    println(0);
  } else {
    println(fact[_x + n - 1] / fact[_x - 1] * power(Mint(d), n));
  }
}
#if defined LOCAL && !defined DUIPAI
  cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
posted @ 2019-12-18 12:24  Pat  阅读(189)  评论(0编辑  收藏  举报