QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#242933#7337. GrasshoppersPetroTarnavskyi#AC ✓3437ms94600kbC++172.9kb2023-11-07 18:52:532023-11-07 18:52:54

Judging History

你现在查看的是最新测评结果

  • [2023-11-07 18:52:54]
  • 评测
  • 测评结果:AC
  • 用时:3437ms
  • 内存:94600kb
  • [2023-11-07 18:52:53]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<LL> VI;
typedef pair<int, int> PII;
typedef long double db;

typedef complex<db> com;

const int LEN = 1 << 20;
com pw[LEN];

void init()
{
	long double phi = 2 * acos(-1.0) / LEN;
	FOR(i, 0, LEN)
		pw[i] = com(cos(i * phi), sin(i * phi));
}

void fft(vector<com>& a, bool inv)
{
	int lg = 0;
	while((1 << lg) < SZ(a)) lg++;
	FOR(i, 0, SZ(a))
	{
		int x = 0;
		FOR(j, 0, lg)
			x |= ((i >> j) & 1) << (lg - j - 1);
		if(i < x)
			swap(a[i], a[x]);
	}
	for(int len = 2; len <= SZ(a); len *= 2)
	{
		int diff = inv ? LEN - LEN / len : LEN / len;
		for(int i = 0; i < SZ(a); i += len)
		{
			int pos = 0;
			FOR(j, 0, len / 2)
			{
				com v = a[i + j];
				com u = a[i + j + len / 2] * pw[pos];
				
				a[i + j] = v + u;
				a[i + j + len / 2] = v - u;
			
			
				pos += diff;
				if(pos >= LEN)
					pos -= LEN;
			}
		}
	}
	if(inv)
	{
		FOR(i, 0, SZ(a))
			a[i] /= SZ(a);
	}
}

LL toInt(db a)
{
	if(abs(a) < 1e-3)
		return 0LL;
	if(a > 0)
		return LL(a + 0.5);
	return -toInt(-a);
}

VI mult(vector<com> a, vector<com> b)
{
	int sz = 0;
	int sum = SZ(a) + SZ(b) - 1;
	while((1 << sz) < sum) sz++;
	a.resize(1 << sz);
	b.resize(1 << sz);
	
	fft(a, false);
	fft(b, false);
	
	FOR(i, 0, SZ(a))
		a[i] *= b[i];
	
	fft(a, true);
	a.resize(sum);
	
	VI res(SZ(a));
	FOR(i, 0, SZ(res))
	{
		res[i] = toInt(a[i].real());
	}
	return res;
}



VI mult(VI a, VI b)
{
	vector<com> A(ALL(a));
	vector<com> B(ALL(b));
	return mult(A, B);
}


int n, m;
VI multNM(VI a, VI b)
{
	VI res = mult(a, b);
	
	for(LL & i : res)
		i %= m;
		
	FOR(i, n, SZ(res))
	{
		res[i % n] += res[i];
	}
	res.resize(min(SZ(res), n));
	
	for(LL &i : res)
	{
		i = (i % m + m) % m;
	}
	return res;
}

VI binpow(VI a, int t)
{
	VI res = {1};
	while(t)
	{
		if(t & 1)
			res = multNM(res, a);
		a = multNM(a, a);
		t /= 2;
	}
	return res;
}


void solve()
{
	int t;
	cin >> n >> m >> t;
	VI a(2 * n);
	FOR(i, 0, n)
	{
		cin >> a[i];
		a[i]--;
		a[i + n] = a[i];
	}
	VI f = binpow({-1, 2}, t);
	reverse(ALL(f));
	VI res = mult(a, f);
	
	FOR(i, 0, n)
	{
		LL ans = res[i + SZ(f) - 1];
		ans = (ans % m + m) % m;
		
		if(i > 0)
			cout << " ";
		cout << ans + 1;
	}
	cout << "\n";
}


int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	cout << fixed << setprecision(15);
	
	init();
	
	//VI res = mult(VI{1, 2, 1}, VI{-1, 1});
	//
	//FOR(i, 0, SZ(res))
	//	cout << res[i] << " ";
	//cout << endl;
	//return 0;
	
	
	
	int t;
	cin >> t;
	while(t--)
		solve();
	
	cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 48ms
memory: 36552kb

input:

2
3 5 1
1 1 2
3 5 4
1 1 2

output:

1 3 5
5 4 5

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 3437ms
memory: 94600kb

input:

92
3 5 5
4 2 3
3 5 5
3 1 5
3 5 5
4 3 5
3 5 5
3 3 2
3 5 5
1 3 3
3 5 5
2 3 3
3 5 5
3 3 2
3 5 5
4 2 5
3 5 5
1 2 4
3 5 5
4 5 1
3 5 5
4 1 4
3 5 5
1 1 2
3 5 5
1 1 4
3 5 5
1 1 4
3 5 5
5 2 4
3 5 5
5 5 1
3 5 5
2 5 5
3 5 5
3 2 5
3 5 5
1 2 3
3 5 5
1 1 1
3 5 5
4 3 1
3 5 5
1 3 4
3 5 5
1 5 1
3 5 5
1 4 4
3 5 5
5 2...

output:

2 1 1
2 5 2
1 5 1
1 3 4
5 4 3
4 1 3
1 3 4
1 1 4
2 5 5
3 3 4
4 2 3
3 1 5
2 1 3
2 1 3
3 3 5
2 5 4
3 4 5
2 4 4
5 5 1
1 1 1
3 5 5
2 4 2
1 2 4
2 3 4
5 3 4
3 4 2
4 3 5
2 5 5
4 4 1
2 5 1
1 1 5
2 5 2
1 5 1
1 1 1
2 2 5
4 3 4
4 2 2
1 2 5
2 5 4
3 3 1
4 4 5
5 5 1
4 3 3
3 4 2
3 4 5
3 2 2
5 2 3
5 1 3
5 1 3
5 2 4
...

result:

ok 112880 numbers

Test #3:

score: 0
Accepted
time: 3247ms
memory: 94592kb

input:

92
3 5 5
4 1 4
3 5 5
3 3 2
3 5 5
2 5 5
3 5 5
5 2 5
3 5 5
2 2 5
3 5 5
4 4 1
3 5 5
1 2 2
3 5 5
2 1 4
3 5 5
3 5 5
3 5 5
5 1 1
3 5 5
5 5 5
3 5 5
3 5 5
3 5 5
5 1 2
3 5 5
2 2 5
3 5 5
5 1 2
3 5 5
2 1 5
3 5 5
5 5 5
3 5 5
2 2 4
3 5 5
3 1 5
3 5 5
2 1 4
3 5 5
5 1 5
3 5 5
4 4 1
3 5 5
3 4 5
3 5 5
5 1 2
3 5 5
1 4...

output:

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