QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#708817 | #2936. Simple Collatz Sequence | sefnuray# | AC ✓ | 1ms | 3704kb | C++14 | 1.5kb | 2024-11-04 07:41:27 | 2024-11-04 07:41:28 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <bitset>
#include <vector>
#include <cmath>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <utility>
#include <deque>
using namespace std;
typedef long long ll;
typedef long double ld;
#define MOD 1000007
int gcd (int a, int b) {
return b ? gcd (b, a % b) : a;
}
void FastDoubling (ll n, ll res[]) {
ll a, b, c, d;
// Base Condition
if (n == 0) {
res[0] = 0;
res[1] = 1;
return;
}
FastDoubling((n / 2), res);
// Here a = F(n)
a = res[0];
// Here b = F(n+1)
b = res[1];
c = 2 * b - a;
if (c < 0)
c += MOD;
// As F(2n) = F(n)[2F(n+1) – F(n)]
// Here c = F(2n)
c = (a * c) % MOD;
// As F(2n + 1) = F(n)^2 + F(n+1)^2
// Here d = F(2n + 1)
d = (a * a + b * b) % MOD;
// Check if N is odd
// or even
if (n % 2 == 0) {
res[0] = c;
res[1] = d;
}
else {
res[0] = d;
res[1] = c + d;
}
}
int main() {
//these first two lines speed up input / output significantly
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n;
cin>>n;
if(n <= 2) {
cout<<"1";
} else {
n = n-2;
ll res[2] = {0};
FastDoubling(n, res);
cout<<((res[0]+res[1]) %1000007);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
input:
6
output:
8
result:
ok single line: '8'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
12345
output:
540591
result:
ok single line: '540591'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
40000
output:
852282
result:
ok single line: '852282'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
2148
output:
427120
result:
ok single line: '427120'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
14386
output:
377717
result:
ok single line: '377717'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
31251
output:
42371
result:
ok single line: '42371'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
33516
output:
503005
result:
ok single line: '503005'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
6167
output:
148870
result:
ok single line: '148870'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
8508
output:
71598
result:
ok single line: '71598'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
18757
output:
910805
result:
ok single line: '910805'
Test #12:
score: 0
Accepted
time: 0ms
memory: 3696kb
input:
29843
output:
982119
result:
ok single line: '982119'
Test #13:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
8618
output:
946465
result:
ok single line: '946465'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
26480
output:
427468
result:
ok single line: '427468'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
23988
output:
536392
result:
ok single line: '536392'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3696kb
input:
39830
output:
852687
result:
ok single line: '852687'
Test #17:
score: 0
Accepted
time: 1ms
memory: 3580kb
input:
10055
output:
571911
result:
ok single line: '571911'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
2227
output:
361051
result:
ok single line: '361051'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
33772
output:
41792
result:
ok single line: '41792'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
2094
output:
41578
result:
ok single line: '41578'