QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#242853#7337. GrasshoppersPetroTarnavskyi#TL 6ms7804kbC++172.8kb2023-11-07 17:51:442023-11-07 17:51:44

Judging History

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

  • [2023-11-07 17:51:44]
  • 评测
  • 测评结果:TL
  • 用时:6ms
  • 内存:7804kb
  • [2023-11-07 17:51:44]
  • 提交

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<int> VI;
typedef pair<int, int> PII;
typedef double db;

typedef complex<db> com;

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

void init()
{
	db 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);
	}
}

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] = int(real(a[i]) + 0.5);
	
	return res;
}





VI mult(VI a, VI b)
{
	VI res(SZ(a) + SZ(b) - 1, 0);
	FOR(i, 0, SZ(a))
		FOR(j, 0, SZ(b))
			res[i + j] += a[i] * b[j];
	return res;
}


int n, m;
VI multNM(VI a, VI b)
{
	VI res = mult(a, b);
	FOR(i, n, SZ(res))
		res[i % n] += res[i];
	res.resize(min(SZ(res), n));
	
	for(int 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(n);
	FOR(i, 0, n)
	{
		cin >> a[i];
		a[i]--;
	}
	VI f = binpow({-1, 2}, t);
	reverse(ALL(f));
	VI res = mult(a, f);
	
	FOR(i, 0, n)
	{
		int 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();
	
	
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 6ms
memory: 7804kb

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: -100
Time Limit Exceeded

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 5 4
2 1 2
1 4 2
1 4 5
5 4 4
4 4 4
1 4 5
1 5 2
2 5 3
3 2 1
4 1 3
3 1 5
2 1 3
2 1 3
3 5 3
2 2 1
3 2 2
2 5 2
5 5 4
1 1 1
3 4 1
2 4 3
1 2 1
2 3 3
5 5 2
3 3 1
4 4 1
2 5 3
4 4 2
2 3 5
1 3 4
2 1 2
1 4 2
1 1 1
2 4 1
4 1 4
4 3 1
1 5 3
2 2 1
3 3 5
4 1 5
5 5 4
4 3 2
3 3 1
3 2 2
3 4 3
5 5 1
5 3 2
5 3 2
5 3 3
...

result: