#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
namespace NTT {
const ll mod = (119 << 23) + 1, root = 3; // = 998244353
// For p < 2^30 there is also e.g. (5 << 25, 3), (7 << 26, 3),
// (479 << 21, 3) and (483 << 21, 5). The last two are > 10^9.
ll modpow(ll b, ll p) { return !p?1:modpow(b*b%mod,p/2)*(p&1?b:1)%mod; }
ll inv (ll b) { return modpow(b,mod-2); }
int get(int s) {
return s > 1 ? 32 - __builtin_clz(s - 1) : 0;
}
vl ntt(vl& a) {
int n = a.size(), x = get(n);
vl res, RES(n), roots(n);
roots[0] = 1, roots[1] = modpow(root,(mod-1)/n);
FOR(i,2,n) roots[i] = roots[i-1]*roots[1] % mod;
res = a;
FOR(i,1,x+1) {
int inc = n>>i;
F0R(j,inc) for (int k = 0; k < n; k += inc) {
int t = 2*k%n+j;
RES[k+j] = (res[t]+roots[k]*res[t+inc]) % mod;
}
swap(res,RES);
}
return res;
}
vl ntt_rev(vl& a) {
vl res = ntt(a);
ll in = inv(a.size());
F0R(i,sz(res)) res[i] = res[i]*in % mod;
reverse(res.begin() + 1, res.end());
return res;
}
vl brute(vl& a, vl& b) {
vl c(sz(a)+sz(b)-1);
F0R(i,sz(a)) F0R(j,sz(b)) c[i+j] = (c[i+j]+a[i]*b[j])%mod;
return c;
}
vl conv(vl a, vl b) {
int s = sz(a)+sz(b)-1, L = get(s), n = 1<<L;
if (s <= 0) return {};
if (s <= 200) return brute(a,b);
a.resize(n); a = ntt(a);
b.resize(n); b = ntt(b);
F0R(i,n) a[i] = a[i]*b[i] % mod;
a = ntt_rev(a);
a.resize(s);
return a;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int a,b; cin >> a >> b;
vl A(a), B(b);
F0R(i,a) {
char c; cin >> c;
A[a-1-i] = c-'0';
}
F0R(i,b) {
char c; cin >> c;
B[b-1-i] = c-'0';
}
vl C = NTT::conv(A,B);
F0R(i,sz(C)) if (C[i] >= 10) {
if (i+1 == sz(C)) C.pb(0);
C[i+1] += C[i]/10; C[i] %= 10;
}
while (sz(C) > 1 && C.back() == 0) C.pop_back();
F0Rd(i,sz(C)) cout << C[i];
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
496 KB |
Output is correct |
3 |
Correct |
2 ms |
496 KB |
Output is correct |
4 |
Correct |
2 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
496 KB |
Output is correct |
3 |
Correct |
2 ms |
496 KB |
Output is correct |
4 |
Correct |
2 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
688 KB |
Output is correct |
9 |
Correct |
2 ms |
692 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
496 KB |
Output is correct |
3 |
Correct |
2 ms |
496 KB |
Output is correct |
4 |
Correct |
2 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
688 KB |
Output is correct |
9 |
Correct |
2 ms |
692 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
5 ms |
1100 KB |
Output is correct |
12 |
Correct |
5 ms |
1148 KB |
Output is correct |
13 |
Correct |
5 ms |
1176 KB |
Output is correct |
14 |
Correct |
10 ms |
1552 KB |
Output is correct |
15 |
Correct |
3 ms |
1552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
496 KB |
Output is correct |
3 |
Correct |
2 ms |
496 KB |
Output is correct |
4 |
Correct |
2 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
572 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
2 ms |
688 KB |
Output is correct |
9 |
Correct |
2 ms |
692 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
5 ms |
1100 KB |
Output is correct |
12 |
Correct |
5 ms |
1148 KB |
Output is correct |
13 |
Correct |
5 ms |
1176 KB |
Output is correct |
14 |
Correct |
10 ms |
1552 KB |
Output is correct |
15 |
Correct |
3 ms |
1552 KB |
Output is correct |
16 |
Correct |
45 ms |
3744 KB |
Output is correct |
17 |
Correct |
90 ms |
6732 KB |
Output is correct |
18 |
Correct |
43 ms |
6732 KB |
Output is correct |
19 |
Correct |
87 ms |
6792 KB |
Output is correct |
20 |
Correct |
39 ms |
6792 KB |
Output is correct |