P6745 『MdOI R3』Number 題解
阿新 • • 發佈:2021-12-23
P6745 『MdOI R3』Number 題解
Content
給定 \(k,x\) ,請將 \(10^k+x\) 轉寫成一般形式。
資料範圍:\(0\leqslant k\leqslant 500, 0\leqslant x\leqslant 10^{18}\)。
Solution
我們看到這裡的 \(k\) 可以到 \(500\) ,因此直接用 \(\texttt{pow}\) 求出來肯定是不現實的,因此需要用字串。
像在這裡的話,我們可以直接把 \(10^k\) 和 \(x\) 分開來儲存到兩個字串裡面,然後的做法類似於高精加,就是把這兩個數加起來就可以了。
總體來說難度不算大。
(Upd on 2021.12.23)這道題目後來又想了一下,發現在 \(x<10^{18}\) ,\(k\geqslant 18\) 的時候並不需要高精加,而是直接將 \(x\) 拼到後面去就可以了。因此,這篇題解不是正解,僅供一個暴力思路的參考。
Code
#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <iostream> using namespace std; int k; long long x; char a[507], b[507]; int main() { scanf("%d%lld", &k, &x); int lena = k + 1, lenb = 0; a[0] = '1'; for(int i = 1; i <= k; ++i) a[i] = '0'; long long p = x; // printf("%d\n", p); int numb[27] = {0}, cnt = 0; while(p) { numb[++cnt] = p % 10; p /= 10; } for(int i = cnt; i >= 1; --i) b[lenb++] = numb[i] + '0'; // puts(""); // printf("%s %s\n", a, b); int aa[507] = {0}, bb[507] = {0}, ans[507] = {0}; for(int i = 0; i < lena; ++i) aa[lena - i] = a[i] - '0'; for(int i = 0; i < lenb; ++i) bb[lenb - i] = b[i] - '0'; int lenc = 1, xx = 0; while(lenc <= lena || lenc <= lenb) { // printf("%d %d %d\n", aa[lenc], bb[lenc], xx); ans[lenc] = aa[lenc] + bb[lenc] + xx; xx = ans[lenc] / 10; ans[lenc] %= 10; // printf("%d\n", ans[lenc]); lenc++; } ans[lenc] = xx; while(!ans[lenc]) lenc--; for(int i = lenc; i >= 1; --i) printf("%d", ans[i]); return 0; }