1. 程式人生 > 其它 >C - 數碼管 (快速冪+費馬小定理)

C - 數碼管 (快速冪+費馬小定理)

有一組壞掉的、只能顯示 0, 2, 5, 6, 8, 9 的 n 位十進位制數碼管,記這組數碼管從左到右讀出的十進位制數為 A(允許有前導零)。由於失誤,這組數碼管被整體旋轉了 180 度,記現在選轉過來的數碼管從左到右讀出的十進位制數為 B(允許有前導零)。比如,原先 A = 025689,現在 B = 689520;原先 A = 66559,現在 B = 65599;原先 A = 02500,現在 B = 00520。A = 025689,B = 689520
在這裡插入圖片描述

現在希望你幫忙計算一下,有多少種方案,使得數碼管旋轉前和旋轉後的讀數相同,即 A = B。由於答案很大,請你給出答案模 998, 244, 353 的值。

Input
一行,一個正整數 n (1 ≤ n < 10105)。

Output
一個非負整數,為答案模 998, 244, 353 的值。

Examples
Input
1
Output
4
Input
2
Output
6
Input
3
Output
24
Input
101
Output
23811823
Input
99999999999999999999999999999999
Output
648334277

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int M = 998244353
; int N = 998244352 * 2; LL solve(LL a, LL b) { LL ans = 1; while (b) { if (b & 1) ans = (ans * a) % M; b >>= 1; a = a * a % M; } return ans; } int main() { LL n, m = 0, ans; char s[100005]; cin >> s; n = strlen(s); for (int i = 0
; i < n; i++) { m = (m * 10 + (s[i] - '0')) % N; } ans = solve(6, m / 2) % M; if (m % 2) ans = ans * 4 % M; cout << ans; }