比赛 2026.5.30 评测结果 AAAWTTTTTT
题目名称 数列求和 最终得分 30
用户昵称 LikableP 运行时间 31.804 s
代码语言 C++ 内存使用 1.51 MiB
提交时间 2026-05-30 11:37:14
显示代码纯文本
#include <cstdio>
typedef long long ll;

ll n, a, k, p;
ll ans;

ll kasumi(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1) res = res * x % p;
    y >>= 1;
    x = x * x % p;
  }
  return res;
}

ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll d = exgcd(b, a % b, x, y);
  ll z = x;
  x = y;
  y = z - (a / b) * y;
  return d;
}

ll inverse(ll num) {
  ll x, y;
  exgcd(num, p, x, y);
  return (x % p + p) % p;
}

int main() {
  freopen("oeis.in", "r", stdin);
  freopen("oeis.out", "w", stdout);
  scanf("%lld %lld %lld %lld", &n, &a, &k, &p);
  if (k == 0) {
    ans = ((kasumi(a, n + 1) - a) % p + p) % p * inverse(a - 1) % p;
    printf("%lld\n", ans);
  } else {
    for (ll i = 1; i <= n; ++i) {
      ans = (ans + kasumi(i, k) * kasumi(a, i)) % p;
    }
    printf("%lld\n", ans);    
  }
  return 0;
}