結果

問題 No.613 Solitude by the window
ユーザー zimpha
提出日時 2017-12-13 01:27:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,672 ms / 2,000 ms
コード長 2,329 bytes
コンパイル時間 619 ms
コンパイル使用メモリ 62,924 KB
最終ジャッジ日時 2025-01-05 05:11:36
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |   scanf("%lld%lld", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
main.cpp:106:15: warning: ‘extra’ may be used uninitialized [-Wmaybe-uninitialized]
  106 |     n %= (end - extra);
      |          ~~~~~^~~~~~~~
main.cpp:67:22: note: ‘extra’ was declared here
   67 |     int64 x = 2 % m, extra, target = -1, end;
      |                      ^~~~~

ソースコード

diff #
プレゼンテーションモードにする

#include <cstdio>
#include <vector>
#include <bitset>
using int64 = long long;
int64 phi(int64 n) {
int64 r = n;
for (int64 i = 2; i * i <= n; ++i) {
if (n % i == 0) {
r = r / i * (i -1);
while (n % i == 0) n /= i;;
}
}
if (n > 1) r = r / n * (n - 1);
return r;
}
int64 pow_mod(int64 a, int64 n, int64 mod) {
int64 r = 1;
for (; n; n >>= 1) {
if (n & 1) r = r * a % mod;
a = a * a % mod;
}
return r;
}
int64 calc(int64 n, int64 mod) {
// (2 + sqrt{3}) ^ (2^n) % mod
n = pow_mod(2, n, mod - 1);
int64 a = 1, b = 0;
int64 c = 2, d = 1;
for (; n; n >>= 1) {
if (n & 1) {
int64 u = (a * c + 3 * b * d) % mod;
int64 v = (a * d + b * c) % mod;
a = u, b = v;
}
int64 u = (c * c + d * d * 3) % mod;
int64 v = 2 * c * d % mod;
c = u, d = v;
}
return a;
}
int mark[34375000];
inline void set(int x) {
mark[x >> 5] |= 1 << (x & 31);
}
inline int get(int x) {
return mark[x >> 5] >> (x & 31) & 1;
}
int main() {
int64 n, m;
scanf("%lld%lld", &n, &m);
if (m == 2) puts("0");
else if (n <= 1000000) {
int64 x = 2;
for (int i = 1; i <= n; ++i) {
x = (x * x + x * 4) % m;
}
printf("%lld\n", x);
} else if (m == 3 || pow_mod(3, m / 2, m) == m - 1) {
int64 x = 2 % m, extra, target = -1, end;
if (m >= 100000000) {
for (int i = 1; i <= n; ++i) {
x = (x * x + 4 * x) % m;
if ((x & 15) == 0) {
if (get(x >> 4)) {
target = x;
end = i;
break;
}
set(x >> 4);
}
}
} else {
set(2 % m);
for (int i = 1; i <= n; ++i) {
x = (x * x + 4 * x) % m;
if (get(x)) {
target = x;
end = i;
break;
}
set(x);
}
}
if (target == -1) {
printf("%lld\n", x);
return 0;
}
x = 2 % m;
for (int i = 0; i < n; ++i) {
if (x == target) {
extra = i;
break;
}
x = (x * x + 4 * x) % m;
}
//printf("%lld %lld %lld\n", extra, end, target);
n -= extra;
n %= (end - extra);
for (int i = 0; i < n; ++i) {
x = (x * x + x * 4) % m;
}
printf("%lld\n", x);
} else {
int64 r = calc(n, m) * 2 % m + m - 2;
printf("%lld\n", r % m);
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0