結果

問題 No.931 Multiplicative Convolution
ユーザー NyaanNyaanNyaanNyaan
提出日時 2019-11-22 21:40:51
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 8,596 bytes
コンパイル時間 2,262 ms
コンパイル使用メモリ 189,956 KB
実行使用メモリ 10,152 KB
最終ジャッジ日時 2024-10-11 03:05:25
合計ジャッジ時間 4,179 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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

#include <bits/stdc++.h>
#define whlie while
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define rep(i,N) for(int i = 0; i < (N); i++)
#define repr(i,N) for(int i = (N) - 1; i >= 0; i--)
#define rep1(i,N) for(int i = 1; i <= (N) ; i++)
#define repr1(i,N) for(int i = (N) ; i > 0 ; i--)
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
using namespace std; void solve();
using ll = long long; using vl = vector<ll>;
using vi = vector<int>; using vvi = vector< vector<int> >;
constexpr int inf = 1001001001;
constexpr ll infLL = (1LL << 61) - 1;
struct IoSetupNya {IoSetupNya() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(7
    );} } iosetupnya;
template<typename T, typename U> inline bool amin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template<typename T, typename U> inline bool amax(T &x, U y) { return (x < y) ? (x = y, true) : false; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { int s = (int)v.size(); rep(i,s) os << (i ? " " : "") << v[i]; return os;
    }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &x : v) is >> x; return is; }
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#ifdef NyaanDebug
#include "NyaanDebug.h"
#define trc(...) do { cerr << #__VA_ARGS__ << " = "; dbg_out(__VA_ARGS__);} while(0)
#define trca(v,N) do { cerr << #v << " = "; array_out(v , N);cout << endl;} while(0)
#else
#define trc(...)
#define trca(...)
int main(){solve();}
#endif
//using P = pair<ll,ll>; using vp = vector<P>;
constexpr int MOD = /** 1000000007; //*/ 998244353;
////////////////
// O( sqrt(N) log log N )
// 0N->1->0
vector<int> Primes(int N){
vector<int> A(N + 1 , 1);
A[0] = A[1] = 0;
for(int i = 2; i * i <= N ; i++)
if(A[i]==1) for(int j = i << 1 ; j <= N; j += i) A[j] = 0;
return A;
}
// O( sqrt(N) log log N )
// 0N->1->
vector<int> Factors(int N){
vector<int> A(N + 1 , 1);
A[0] = A[1] = 0;
for(int i = 2; i * i <= N ; i++)
if(A[i]==1) for(int j = i << 1 ; j <= N; j += i) A[j] = i;
return A;
}
// φ(N)=(NN)
vector<int> EulersTotientFunction(int N){
vector<int> ret(N + 1 , 0);
for(int i = 0; i <= N ; i++) ret[i] = i;
for(int i = 2 ; i <= N ; i++){
if(ret[i] == i)
for(int j = i; j <= N; j += i) ret[j] = ret[j] / i * (i - 1);
}
return ret;
}
// O(sqrt(N))
// N
vector<long long> Divisor(long long N){
vector<long long> v;
for(long long i = 1; i * i <= N ; i++){
if(N % i == 0){
v.push_back(i);
if(i * i != N) v.push_back(N / i);
}
}
return v;
}
//
// keyvaluemap
// ex) N=12 -> m={ (2,2) , (3,1) }
map<long long,int> PrimeFactors(long long N){
map<long long,int> m;
for(long long i=2; i * i <= N; i++)
while(N % i == 0) m[i]++ , N /= i;
if(N != 1) m[N]++;
return m;
}
// modr調
bool PrimitiveRoot(long long r , long long mod){
r %= mod; if(r == 0) return false;
auto modpow = [](long long a,long long b,long long m)->long long{
a %= m; long long ret = 1;
while(b){
if(b & 1) ret = a * ret % m;
a = a * a % m;
b >>= 1;
}
return ret;
};
map<long long,int> m = PrimeFactors(mod - 1);
each(x , m){
if(modpow(r , (mod - 1) / x.fi , mod ) == 1) return false;
}
return true;
}
// ax+by=gcd(a,b)
//  
long long extgcd(long long a,long long b, long long &x, long long &y){
if(b == 0){
x = 1; y = 0; return a;
}
long long d = extgcd(b , a%b , y , x);
y -= a / b * x;
return d;
}
//
// Point. -1 (UNIT & a = aUNIT)
struct BA{
unsigned long long x;
BA(): x(0){}
BA(unsigned long long y):x(y){}
BA operator += (const BA &p){
x = x ^ p.x;
return (*this);
}
BA operator *= (const BA &p){
x = x & p.x;
return (*this);
}
BA operator+(const BA &p)const {return BA(*this) += p;}
BA operator*(const BA &p)const {return BA(*this) *= p;}
bool operator==(const BA &p) const { return x == p.x; }
bool operator!=(const BA &p) const { return x != p.x; }
friend ostream &operator<<(ostream &os,const BA &p){
return os << p.x;
}
friend istream &operator>>(istream &is, BA &a){
unsigned int t;
is >> t;
a = BA(t);
return (is);
}
};
template< int mod >
struct NumberTheoreticTransform {
vector< int > rev, rts;
int base, max_base, root;
NumberTheoreticTransform() : base(1), rev{0, 1}, rts{0, 1} {
assert(mod >= 3 && mod % 2 == 1);
auto tmp = mod - 1;
max_base = 0;
while(tmp % 2 == 0) tmp >>= 1, max_base++;
root = 2;
while(mod_pow(root, (mod - 1) >> 1) == 1) ++root;
assert(mod_pow(root, mod - 1) == 1);
root = mod_pow(root, (mod - 1) >> max_base);
}
inline int mod_pow(int x, int n) {
int ret = 1;
while(n > 0) {
if(n & 1) ret = mul(ret, x);
x = mul(x, x);
n >>= 1;
}
return ret;
}
inline int inverse(int x) {
return mod_pow(x, mod - 2);
}
inline unsigned add(unsigned x, unsigned y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
inline unsigned mul(unsigned a, unsigned b) {
return 1ull * a * b % (unsigned long long) mod;
}
void ensure_base(int nbase) {
if(nbase <= base) return;
rev.resize(1 << nbase);
rts.resize(1 << nbase);
for(int i = 0; i < (1 << nbase); i++) {
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
}
assert(nbase <= max_base);
while(base < nbase) {
int z = mod_pow(root, 1 << (max_base - 1 - base));
for(int i = 1 << (base - 1); i < (1 << base); i++) {
rts[i << 1] = rts[i];
rts[(i << 1) + 1] = mul(rts[i], z);
}
++base;
}
}
void ntt(vector< int > &a) {
const int n = (int) a.size();
assert((n & (n - 1)) == 0);
int zeros = __builtin_ctz(n);
ensure_base(zeros);
int shift = base - zeros;
for(int i = 0; i < n; i++) {
if(i < (rev[i] >> shift)) {
swap(a[i], a[rev[i] >> shift]);
}
}
for(int k = 1; k < n; k <<= 1) {
for(int i = 0; i < n; i += 2 * k) {
for(int j = 0; j < k; j++) {
int z = mul(a[i + j + k], rts[j + k]);
a[i + j + k] = add(a[i + j], mod - z);
a[i + j] = add(a[i + j], z);
}
}
}
}
vector< int > multiply(vector< int > a, vector< int > b) {
int need = a.size() + b.size() - 1;
int nbase = 1;
while((1 << nbase) < need) nbase++;
ensure_base(nbase);
int sz = 1 << nbase;
a.resize(sz, 0);
b.resize(sz, 0);
ntt(a);
ntt(b);
int inv_sz = inverse(sz);
for(int i = 0; i < sz; i++) {
a[i] = mul(a[i], mul(b[i], inv_sz));
}
reverse(a.begin() + 1, a.end());
ntt(a);
a.resize(need);
return a;
}
};
void solve(){
ini(P);
vi A(P-1) , B(P-1); in(A , B);
ll pr = 1;
while(PrimitiveRoot(pr , P) == false) pr++;
vi p(P-1);
p[0] = 1;
trc(p);
rep1(i , P-2) p[i] = 1LL * p[i-1] * pr % P;
vi inv(P);
//rep(i,P-1) inv[p[i]] = i ;
trc(p);
vi s(P-1) , t(P-1);
rep(i , P-1){
s[i] = A[p[i] - 1];
t[i] = B[p[i] - 1];
}
trc(s,t);
NumberTheoreticTransform<MOD> ntt;
auto u = ntt.multiply(s , t);
trc(u);
for(int i = P-1 ; i < u.size();i++) u[i%(P-1)] = (u[i%(P-1)] + u[i]) % MOD;
trc(u);
trc(inv);
vi ans(P-1);
rep(i,P-1) ans[p[i] - 1] = u[i];
out(ans);
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0