QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#317143 | #7308. Permutation and noitatumreP | rgnerdplayer# | AC ✓ | 87ms | 3808kb | C++20 | 3.8kb | 2024-01-28 16:26:07 | 2024-01-28 16:26:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <int P>
struct Modint {
int v;
constexpr Modint() : v(0) {}
constexpr Modint(i64 v_) : v(v_ % P) {
if (v < 0) {
v += P;
}
}
constexpr explicit operator int() const { return v; }
constexpr friend ostream& operator<<(ostream &out, Modint n) {
return out << int(n);
}
constexpr friend istream& operator>>(istream &in, Modint &n) {
i64 v;
in >> v;
n = Modint(v);
return in;
}
constexpr friend bool operator==(Modint a, Modint b) {
return a.v == b.v;
}
constexpr friend bool operator!=(Modint a, Modint b) {
return a.v != b.v;
}
constexpr Modint operator-() {
Modint res;
res.v = v ? P - v : 0;
return res;
}
constexpr Modint& operator++() {
v++;
if (v == P) v = 0;
return *this;
}
constexpr Modint& operator--() {
if (v == 0) v = P;
v--;
return *this;
}
constexpr Modint& operator+=(Modint o) {
v -= P - o.v;
v = (v < 0) ? v + P : v;
return *this;
}
constexpr Modint& operator-=(Modint o) {
v -= o.v;
v = (v < 0) ? v + P : v;
return *this;
}
constexpr Modint& operator*=(Modint o) {
v = 1LL * v * o.v % P;
return *this;
}
constexpr Modint& operator/=(Modint o) { return *this *= o.inv(); }
constexpr friend Modint operator++(Modint &a, int) {
Modint r = a;
++a;
return r;
}
constexpr friend Modint operator--(Modint &a, int) {
Modint r = a;
--a;
return r;
}
constexpr friend Modint operator+(Modint a, Modint b) {
return Modint(a) += b;
}
constexpr friend Modint operator-(Modint a, Modint b) {
return Modint(a) -= b;
}
constexpr friend Modint operator*(Modint a, Modint b) {
return Modint(a) *= b;
}
constexpr friend Modint operator/(Modint a, Modint b) {
return Modint(a) /= b;
}
constexpr Modint qpow(i64 p) {
Modint res = 1, x = v;
while (p > 0) {
if (p & 1) { res *= x; }
x *= x;
p >>= 1;
}
return res;
}
constexpr Modint inv() {
return qpow(P - 2);
}
};
constexpr int P = 1e9 + 7;
using Mint = Modint<P>;
vector<Mint> operator*(vector<Mint> a, vector<Mint> b) {
if (a.empty() || b.empty()) { return {}; }
int n = a.size(), m = b.size();
vector<Mint> c(n + m - 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
c[i + j] += a[i] * b[j];
}
}
return c;
}
// p : a[0] ~ a[d - 1]
// q : \sum a[i - j]q[j] = 0
template <typename T>
T linearRecurrence(vector<T> p, vector<T> q, i64 n) {
int d = q.size() - 1;
assert(int(p.size()) == d);
p = p * q;
p.resize(d);
while (n > 0) {
auto nq = q;
for (int i = 1; i <= d; i += 2) {
nq[i] *= -1;
}
auto np = p * nq;
nq = q * nq;
for (int i = 0; i < d; i++) {
p[i] = np[i * 2 + n % 2];
}
for (int i = 0; i <= d; i++) {
q[i] = nq[i * 2];
}
n /= 2;
}
return p[0] / q[0];
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
auto solve = [&]() {
if (n == 1) {
cout << "1\n";
return;
}
Mint ans = linearRecurrence<Mint>({2, 6, 16, 36}, {1, -3, 2, -1, 1}, n - 2);
cout << ans << '\n';
};
while (cin >> n) {
solve();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3808kb
input:
4 1000000000
output:
16 861159011
result:
ok 2 number(s): "16 861159011"
Test #2:
score: 0
Accepted
time: 87ms
memory: 3584kb
input:
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ...
output:
1 2 6 16 36 80 178 394 870 1920 4236 9344 20610 45458 100262 221136 487732 1075728 2372594 5232922 11541574 25455744 56144412 123830400 273116546 602377506 328585407 930287362 462952218 254489838 439267033 341486279 937462398 314191817 969869915 877202216 68596237 107062384 91326979 251250197 609562...
result:
ok 20000 numbers
Extra Test:
score: 0
Extra Test Passed