Language: C++

#ifndef __FFT_H__
#define __FFT_H__

/* updated: 2020-03-28 */

// required: Sets

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "Sets.h"
#endif

#include <cstdint>
#include <iostream>
#include <vector>
#include <utility>
#include <cassert>
#include <algorithm>
#include <complex>

class MyComplex {
	using Real = double;
	Real re;
	Real im;

public:
	MyComplex();
	explicit MyComplex(Real re);
	explicit MyComplex(Real re, Real im);
	explicit MyComplex(const std::complex<double>& other);
	MyComplex(const MyComplex&) = default;
	MyComplex& operator=(const MyComplex&) = default;

	MyComplex& operator+=(const MyComplex& right);
	MyComplex& operator-=(const MyComplex& right);
	MyComplex& operator*=(const MyComplex& right);
	MyComplex& operator/=(const MyComplex& right);

	static MyComplex multiply(const MyComplex& left, const MyComplex& right);
	static MyComplex divide(const MyComplex& left, const MyComplex& right);

	Real real() const;
	Real image() const;
};

MyComplex operator+(const MyComplex& left, const MyComplex& right);
MyComplex operator-(const MyComplex& left, const MyComplex& right);
MyComplex operator*(const MyComplex& left, const MyComplex& right);
MyComplex operator/(const MyComplex& left, const MyComplex& right);

void uMul64(uint64_t a, uint64_t b, uint64_t* ret_lo, uint64_t* ret_hi);

// 逆元は高速化できる？
class IntModSpecial {
	// value_m must be less than Mod
	uint64_t value_m;
public:
	static const uint64_t Mod = 0xFFFFFFFF00000001;

	IntModSpecial();
	IntModSpecial(uint64_t value);
	IntModSpecial(const IntModSpecial&) = default;
	IntModSpecial& operator=(const IntModSpecial&) = default;

	IntModSpecial& operator+=(const IntModSpecial& right);
	IntModSpecial& operator-=(const IntModSpecial& right);
	IntModSpecial& operator*=(const IntModSpecial& right);
	IntModSpecial& operator/=(const IntModSpecial& right);

	uint64_t get() const;
	IntModSpecial power(uint64_t exp) const;
	IntModSpecial inverse() const;
	static IntModSpecial primitiveRoot(int rank);
};

IntModSpecial operator+(const IntModSpecial& left, const IntModSpecial& right);
IntModSpecial operator-(const IntModSpecial& left, const IntModSpecial& right);
IntModSpecial operator*(const IntModSpecial& left, const IntModSpecial& right);
IntModSpecial operator/(const IntModSpecial& left, const IntModSpecial& right);

std::istream& operator>>(std::istream& ist, IntModSpecial& object);
std::ostream& operator<<(std::ostream& ost, const IntModSpecial& object);

//uint32_t bitReverse(uint32_t val);
//uint32_t bitReverse(uint32_t val, int digits);

/*------------------------------------------*/

/* もう少し良い方法はないか */
template <uint32_t Mod>
struct Divisors {
	static constexpr int calcSize() {
		uint32_t n = Mod;
		int count = 0;
		for (uint32_t i = 2; i * i <= n; ++i) {
			if (n % i == 0) {
				++count;
			}

			while (n % i == 0) {
				n /= i;
			}
		}

		if (n > 1) {
			++count;
		}

		return count;
	}

	constexpr static int size = calcSize();
	uint32_t list[size];

	constexpr Divisors() : list() {
		uint32_t n = Mod;
		int index = 0;
		for (uint32_t i = 2; i * i <= n; ++i) {
			if (n % i == 0) {
				list[index] = i;
				++index;
			}

			while (n % i == 0) {
				n /= i;
			}
		}

		if (n > 1) {
			list[index] = n;
			++index;
		}
	}
};

constexpr uint32_t powMod(uint32_t base, uint64_t exp, uint32_t modulus) {
	uint64_t product = 1;
	uint64_t multiplier = base;
	while (exp > 0) {
		if (exp % 2 == 1) {
			product = product * multiplier % modulus;
		}
		multiplier = multiplier * multiplier % modulus;
		exp /= 2;
	}
	return uint32_t(product);
}

/* 素数 Mod に対して原始根を一つ求める */
template <uint32_t Mod>
constexpr uint32_t primitiveRoot() {
	const uint32_t order = Mod - 1;
	Divisors<order> divisors;
	for (int i = 2; i < Mod; ++i) {
		bool is_primitive = true;
		for (uint32_t factor : divisors.list) {
			if (powMod(i, order / factor, Mod) == 1) {
				is_primitive = false;
				break;
			}
		}

		if (is_primitive) {
			return i;
		}
	}
}

template <class T, uint32_t Rank>
struct FFTRing {
	static_assert(Rank == 0 && Rank != 0, "Type T not supported");
	// T : 値型(複素数や有限体など)
	// root() : 1 の length 乗根
};

template <uint32_t Mod, uint32_t Rank>
struct FFTRing<IntMod<Mod>, Rank> {
	using T = IntMod<Mod>;
	static constexpr uint32_t rank = Rank;
	static constexpr uint32_t length = 1u << Rank;
	static constexpr uint32_t order = (Mod - 1) & (uint32_t)(-int32_t(Mod - 1));
	static constexpr uint32_t primitive = primitiveRoot<Mod>();
	static_assert(Rank < 32 && length <= order, "array too large");

	static T root() {
		const uint32_t coeff = (Mod - 1) / order;
		return T(primitive).power(uint64_t(coeff) * (order / length));
	}
};

template <uint32_t Rank>
struct FFTRing<IntModSpecial, Rank> {
	using T = IntModSpecial;
	static constexpr uint32_t rank = Rank;
	static constexpr uint32_t length = 1u << Rank;
	static_assert(Rank < 32, "array too long");

	static T root() {
		return T::primitiveRoot(Rank);
	}
};

template <uint32_t Rank>
struct FFTRing<MyComplex, Rank> {
	using T = MyComplex;
	static constexpr uint32_t rank = Rank;
	static constexpr uint32_t length = 1u << Rank;

	static T root() {
		const double PI = std::acos(-1);
		return std::polar(1.0, 2 * PI / length);
	}
};

/*----------------------------------------*/
/*--------   FourierTransformer   --------*/
/*----------------------------------------*/

template <class T, uint32_t Rank>
class FourierTransformer {
	using Ring = FFTRing<T, Rank>;

	static constexpr uint32_t length = 1u << Rank;
	static_assert(Rank < 32, "array too long");

	const T length_inv;

	// 2つ分配列をとるのは無駄かもしれない
	const std::vector<T> roots;
	const std::vector<T> roots_inv;

public:
	FourierTransformer();

private:
	std::vector<T> calcTable(bool inverse);

public:
	std::vector<T> generateArray() const;
	void execute(std::vector<T>& array) const;
	void inverse(std::vector<T>& array) const;
	std::vector<T> convolve(const std::vector<T>& a, const std::vector<T>& b) const;

private:
	//void bitReverseArray(std::vector<T>& arr) const;
	void butterfly(T& a, T& b, const T& root) const;
	void transform(std::vector<T>& arr, const std::vector<T>& roots) const;
};

template <class T, uint32_t Rank>
FourierTransformer<T, Rank>::FourierTransformer()
	: length_inv(T(1) / T(length)), 
	roots(calcTable(false)),
	roots_inv(calcTable(true)) {
}

template <class T, uint32_t Rank>
std::vector<T> FourierTransformer<T, Rank>::calcTable(bool inverse) {
	std::vector<T> table(length + 1);
	const T root = Ring::root();

	table[0] = T(1);
	for (int i = 1; i <= length; ++i) {
		table[i] = table[i - 1] * root;
	}

	if (inverse) {
		std::reverse(table.begin(), table.end());
	}
	return table;
}

template <class T, uint32_t Rank>
std::vector<T> FourierTransformer<T, Rank>::generateArray() const {
	return std::vector<T>(length);
}

template <class T, uint32_t Rank>
void FourierTransformer<T, Rank>::execute(std::vector<T>& array) const {
	assert(array.size() == length);
	transform(array, roots);
}

template <class T, uint32_t Rank>
void FourierTransformer<T, Rank>::inverse(std::vector<T>& array) const {
	assert(array.size() == length);
	transform(array, roots_inv);

	for (T& element : array) {
		element *= length_inv;
	}
}

template <class T, uint32_t Rank>
std::vector<T> FourierTransformer<T, Rank>::convolve(const std::vector<T>& a, const std::vector<T>& b) const {
	assert(a.size() == length && b.size() == length);

	std::vector<T> a_copied = a;
	std::vector<T> b_copied = b;

	execute(a_copied);
	execute(b_copied);

	for (int i = 0; i < length; ++i) {
		a_copied[i] *= b_copied[i];
	}

	inverse(a_copied);
	return a_copied;
}

//template <class T, uint32_t Rank>
//void FourierTransformer<T, Rank>::bitReverseArray(std::vector<T>& arr) const {
//	for (uint32_t i = 0; i < length; ++i) {
//		uint32_t j = bitReverse(i, Rank);
//		if (j > i) {
//			std::swap(arr[i], arr[j]);
//		}
//	}
//}

template <class T, uint32_t Rank>
void FourierTransformer<T, Rank>::butterfly(T& a, T& b, const T& root) const {
	T temp = b * root;
	b = a - temp;
	a += temp;
}

/* FFT の本体 */
template <class T, uint32_t Rank>
void FourierTransformer<T, Rank>::transform(std::vector<T>& arr, const std::vector<T>& roots) const {
	bitReverseArray(arr);
	for (int cnt = 0; cnt < Rank; ++cnt) {
		const int blk_max = length >> (cnt + 1);
		const int blk_size = 1 << (cnt + 1);
		const int root_interval = blk_max;

		for (int blk = 0; blk < blk_max; ++blk) {
			const int idx_max = 1 << cnt;

			for (int idx = 0; idx < idx_max; ++idx) {
				const int left = (blk * blk_size + idx);
				const int right = left + blk_size / 2;

				butterfly(arr[left], arr[right], roots[idx * root_interval]);
			}
		}
	}
}

#endif


#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h&& j >= 0 && j < w; }

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "Interpolation.h"
#include "IntMod.h"
#include "KDTree.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "List.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

const int P = 200003;
using MP = IntMod<P>;

vector<LL> explist() {
	vector<LL> ret(P);
	MP prod = 1;
	MP g = 2;
	REP(i, 0, P - 1) {
		ret[i] = prod.get();
		prod *= g;
	}
	return ret;
}

vector<LL> loglist() {
	vector<LL> ret(P);
	MP prod = 1;
	MP g = 2;
	REP(i, 0, P - 1) {
		ret[prod.get()] = i;
		prod *= g;
	}
	return ret;
}

int main() {
	int n = Next<int>();
	vector<LL> v(n);
	REP(i, 0, n) v[i] = Next<LL>();
	vector<LL> lg = loglist();
	vector<LL> ex = explist();

	FourierTransformer<IntModSpecial, 19> ft;
	auto a = ft.generateArray();

	REP(i, 0, n) {
		if (v[i] == 0) continue;
		int pos = lg[v[i]];
		a[pos] += 1;
	}
	auto res = ft.convolve(a, a);

	LL sum = 0;
	REP(i, 0, res.size()) {
		sum += res[i].get() * ex[i % (P - 1)];
	}

	REP(i, 0, n) {
		sum -= v[i] * v[i] % P;
	}

	cout << sum / 2 << endl;
	return 0;
}





Compiler output:
a.cc:177:16: error: 'IntMod' was not declared in this scope
  177 | struct FFTRing<IntMod<Mod>, Rank> {
      |                ^~~~~~
a.cc:177:26: error: wrong number of template arguments (1, should be 2)
  177 | struct FFTRing<IntMod<Mod>, Rank> {
      |                          ^
a.cc:170:8: note: provided for 'template<class T, unsigned int Rank> struct FFTRing'
  170 | struct FFTRing {
      |        ^~~~~~~
a.cc: In static member function 'static FFTRing<MyComplex, Rank>::T FFTRing<MyComplex, Rank>::root()':
a.cc:211:34: error: could not convert 'std::polar<double>(1.0e+0, ((2 * PI) / FFTRing<MyComplex, Rank>::length))' from 'std::complex<double>' to 'FFTRing<MyComplex, Rank>::T' {aka 'MyComplex'}
  211 |                 return std::polar(1.0, 2 * PI / length);
      |                        ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
      |                                  |
      |                                  std::complex<double>
a.cc: At global scope:
a.cc:474:12: error: 'IntMod' does not name a type
  474 | using MP = IntMod<P>;
      |            ^~~~~~
a.cc: In function 'std::vector<long int> explist()':
a.cc:478:9: error: 'MP' was not declared in this scope; did you mean 'PP'?
  478 |         MP prod = 1;
      |         ^~
      |         PP
a.cc:479:11: error: expected ';' before 'g'
  479 |         MP g = 2;
      |           ^~
      |           ;
a.cc:481:26: error: 'prod' was not declared in this scope
  481 |                 ret[i] = prod.get();
      |                          ^~~~
a.cc:482:25: error: 'g' was not declared in this scope
  482 |                 prod *= g;
      |                         ^
a.cc: In function 'std::vector<long int> loglist()':
a.cc:489:9: error: 'MP' was not declared in this scope; did you mean 'PP'?
  489 |         MP prod = 1;
      |         ^~
      |         PP
a.cc:490:11: error: expected ';' before 'g'
  490 |         MP g = 2;
      |           ^~
      |           ;
a.cc:492:21: error: 'prod' was not declared in this scope
  492 |                 ret[prod.get()] = i;
      |                     ^~~~
a.cc:493:25: error: 'g' was not declared in this scope
  493 |                 prod *= g;
      |                         ^
a.cc: In instantiation of 'void FourierTransformer<T, Rank>::transform(std::vector<T>&, const std::vector<T>&) const [with T = IntModSpecial; unsigned int Rank = 19]':
a.cc:281:2:   required from 'void FourierTransformer<T, Rank>::execute(std::vector<T>&) const [with T = IntModSpecial; unsigned int Rank = 19]'
  281 |         transform(array, roots);
      |         ^~~~~~~~~
a.cc:301:2:   required from 'std::vector<T> FourierTransformer<T, Rank>::convolve(const std::vector<T>&, const std::vector<T>&) const [with T = IntModSpecial; unsigned int Rank = 19]'
  301 |         execute(a_copied);
      |         ^~~~~~~
a.cc:513:24:   required from here
  513 |         auto res = ft.convolve(a, a);
      |                    ~~~~~~~~~~~^~~~~~
a.cc:332:24: error: 'bitReverseArray' was not declared in this scope
  332 |         bitReverseArray(arr);
      |         ~~~~~~~~~~~~~~~^~~~~