QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#638430 | #9225. Fibonacci Fusion | propane# | WA | 3971ms | 249164kb | C++20 | 4.9kb | 2024-10-13 15:54:53 | 2024-10-13 15:55:04 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<numeric>
#include<set>
#include<map>
using namespace std;
using LL = long long;
const int BASE = 1e9, WIDTH = 9;
using BigInt = vector<int>;
BigInt operator+(const BigInt &a, const BigInt &b){
BigInt c(max(a.size(), b.size()) + 1);
for(int i = 0; i < (int)a.size(); i++){
c[i] += a[i];
}
for(int i = 0; i < (int)b.size(); i++){
c[i] += b[i];
}
for(int i = 0; i < (int)c.size(); i++){
if (c[i] >= BASE){
c[i + 1] += 1;
c[i] -= BASE;
}
}
while(c.size() > 1 && c.back() == 0){
c.pop_back();
}
return c;
}
BigInt operator-(const BigInt &a, const BigInt &b){
BigInt c(max(a.size(), b.size()));
for(int i = 0; i < (int)a.size(); i++){
c[i] += a[i];
}
for(int i = 0; i < (int)b.size(); i++){
c[i] -= b[i];
}
for(int i = 0; i < (int)c.size(); i++){
if (c[i] < 0){
c[i + 1] -= 1;
c[i] += BASE;
}
}
while(c.size() > 1 && c.back() == 0){
c.pop_back();
}
return c;
}
BigInt operator*(const BigInt &a, int b){
BigInt c(a.size() + 1);
long long t = 0;
for(int i = 0; i < (int)a.size(); i++){
t += 1LL * a[i] * b;
c[i] = t % BASE;
t /= BASE;
}
c.back() = t;
while(c.size() > 1 && c.back() == 0){
c.pop_back();
}
return c;
}
pair<BigInt, long long> operator/(const BigInt &a, int b){
BigInt c;
c.reserve(a.size());
long long r = 0;
for(int i = (int)a.size() - 1; i >= 0; i--){
r = r * BASE + a[i];
c.push_back(r / b);
r %= b;
}
reverse(c.begin(), c.end());
while(c.size() > 1 && c.back() == 0){
c.pop_back();
}
return {c, r};
}
bool cmp(const BigInt &a, const BigInt &b){
if (a.size() != b.size()){
return a.size() < b.size();
}
for(int i = (int)a.size() - 1; i >= 0; i--){
if (a[i] != b[i]){
return a[i] < b[i];
}
}
return false;
}
BigInt str2int(const string &s){
BigInt a;
a.reserve(s.size() / WIDTH + 1);
int base = 1, val = 0;
for(int i = (int)s.size() - 1; i >= 0; i--){
val += (s[i] - '0') * base;
base *= 10;
if (base == BASE){
a.push_back(val);
base = 1, val = 0;
}
}
if (a.empty() || val) a.push_back(val);
return a;
}
ostream &operator<<(ostream &os, const BigInt &A) {
os << A.back();
for(int i = (int)A.size() - 2; i >= 0; i--)
os << setw(WIDTH) << setfill('0') << A[i];
return os;
}
const int N = 5000, M = 4e6;
BigInt fib[N + 5];
int fib1[M + 5], fib2[M + 5];
const int mod1 = 1e9 + 7, mod2 = 998244353;
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
fib[1] = str2int("1");
fib[2] = str2int("2");
fib1[1] = fib2[1] = 1;
fib1[2] = fib2[2] = 2;
for(int i = 3; i <= N; i++){
fib[i] = fib[i - 2] + fib[i - 1];
}
for(int i = 3; i <= M; i++){
fib1[i] = (fib1[i - 2] + fib1[i - 1]) % mod1;
fib2[i] = (fib2[i - 2] + fib2[i - 1]) % mod2;
}
int n;
cin >> n;
vector<BigInt> a(n);
vector<int> a1(n), a2(n);
for(int i = 0; i < n; i++){
string s;
cin >> s;
a[i] = str2int(s);
int v1 = 0, v2 = 0;
for(auto c : s){
v1 = (v1 * 10LL + c - '0') % mod1;
v2 = (v2 * 10LL + c - '0') % mod2;
}
a1[i] = v1;
a2[i] = v2;
}
vector<int> id(n);
iota(id.begin(), id.end(), 0);
sort(id.begin(), id.end(), [&](int x, int y){
return cmp(a[x], a[y]);
});
set<pair<int, int> > s;
for(int i = 1; i <= M; i++){
s.insert({fib1[i], fib2[i]});
}
LL ans = 0;
map<pair<int, int>, int> mp;
for(int i = 0; i < n; i++){
int x = id[i];
if (cmp(fib[N - 2], a[x])){
for(int j = 0; j < i; j++){
int v1 = (a1[id[j]] + a1[x]) % mod1;
int v2 = (a2[id[j]] + a2[x]) % mod2;
if (s.contains({v1, v2})){
ans += 1;
}
}
}
else{
int rk = lower_bound(fib + 1, fib + N + 1, a[x], cmp) - fib;
for(auto y : {rk, rk + 1}){
int v1 = (fib1[y] - a1[x] + mod1) % mod1;
int v2 = (fib2[y] - a2[x] + mod2) % mod2;
if (mp.contains({v1, v2})){
ans += mp[{v1, v2}];
}
}
}
mp[{a1[x], a2[x]}] += 1;
}
cout << ans << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 3432ms
memory: 223744kb
input:
6 50 8 8 5 72 354224848179261915070
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 3343ms
memory: 225848kb
input:
28 200878223506436882933619847964496455022155117513398820563747455993172799881403389571477889821109288771413214004090719097929400406252135763028179112130390003528046316900603668569910008417315162907579003880220844686222148696041857432602133894827753998572080650383305777912447151917272483538029469449...
output:
27
result:
ok 1 number(s): "27"
Test #3:
score: 0
Accepted
time: 3466ms
memory: 226412kb
input:
5187 2640352926124261912741724778991366987330659389621881876017670644497364093930668042530271338851702874394631009332660937266680740235862107353443518003194307853104942996827176097428402408674756368623972812842571069642405111849826172879369776309763468485788964245903781380419155348915131587410703749...
output:
6073
result:
ok 1 number(s): "6073"
Test #4:
score: 0
Accepted
time: 3423ms
memory: 236556kb
input:
200000 2 2 2 2 1 2 1 1 2 2 1 1 1 2 2 1 1 2 1 1 2 2 1 2 2 2 1 1 1 1 2 2 1 2 1 2 1 1 2 2 1 1 1 2 1 1 2 1 2 2 2 2 1 2 2 1 1 1 2 1 1 1 1 1 2 1 2 2 1 1 1 2 2 2 1 1 2 1 1 2 1 2 1 1 1 2 2 2 1 1 1 1 2 1 2 1 1 2 2 1 1 2 1 1 2 1 2 2 1 2 1 2 2 1 1 2 1 1 1 2 2 2 1 2 2 1 1 2 2 2 2 1 2 1 1 2 1 2 2 1 1 1 1 2 2 2 2...
output:
15003749259
result:
ok 1 number(s): "15003749259"
Test #5:
score: 0
Accepted
time: 3784ms
memory: 249164kb
input:
200000 944176313232170622314 2590599414036674999101 753315073608896000424 9299685298577430049245 9361800333778142620806 8988699166328904060999 9606920674025578304023 4203331868598952026136 5183047027116137697788 3968714342776915029801 8130984095583566992354 3206443643596048048798 6248561214283254355...
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: -100
Wrong Answer
time: 3971ms
memory: 237416kb
input:
200000 9 10 3 5 9 3 3 9 8 5 1 2 7 8 4 6 2 3 3 9 5 5 4 9 7 5 8 2 6 10 9 7 2 2 1 10 10 6 10 7 4 7 9 7 2 2 10 4 5 8 2 2 5 8 9 5 3 9 2 1 7 6 8 8 6 3 8 2 2 9 10 2 9 7 1 9 1 4 5 9 2 7 10 1 8 7 4 8 1 10 6 4 4 9 1 9 7 3 6 5 6 9 5 3 6 6 4 4 6 1 8 6 10 3 10 2 1 4 1 4 8 2 9 1 4 8 10 8 2 2 3 6 4 7 10 10 9 4 7 6...
output:
4388465666
result:
wrong answer 1st numbers differ - expected: '4388485679', found: '4388465666'