QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#780252#6179. 最大平方问题le0n10 2033ms17148kbC++144.0kb2024-11-25 09:17:512024-11-25 09:17:53

Judging History

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

  • [2024-11-25 09:17:53]
  • 评测
  • 测评结果:10
  • 用时:2033ms
  • 内存:17148kb
  • [2024-11-25 09:17:51]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1e6 + 5, mod = 998244353;
int p;
mt19937 rng(1293128);
map<ll, int> mp;
int qpow(int x, int y)
{
	int ans = 1;
	while(y)
	{
		if(y & 1)
			ans = (ll)ans * x % mod;
		x = (ll)x * x % mod;
		y >>= 1;
	}
	return ans;
}
int qpow_p(int x, int y)
{
	int ans = 1;
	while(y)
	{
		if(y & 1)
			ans = (ll)ans * x % p;
		x = (ll)x * x % p;
		y >>= 1;
	}
	return ans;
}
void mul_f(vector<int> &A, vector<int> B)
{
	int i, j, n, m;
	if(A.empty() || B.empty())
	{
		A.clear();
		return ;
	}
	n = (int)A.size() - 1;
	m = (int)B.size() - 1;
	A.resize(n + m + 1);
	for(i = n + 1; i <= n + m; i++)
		A[i] = 0;
	for(i = n; i >= 0; i--)
	{
		for(j = 1; j <= m; j++)
			A[i + j] = (A[i + j] + (ll)A[i] * B[j]) % mod;
		A[i] = (ll)A[i] * B[0] % mod;
	}
}
void mod_f(vector<int> &A, vector<int> B)
{
	int i, j, q, n, m;
	n = (int)A.size() - 1;
	m = (int)B.size() - 1;
	q = qpow_p(B.back(), p - 2);
	for(auto &o: B)
		o = (ll)o * q % mod;
	for(i = n; i >= m; i--)
	{
		q = p - A[i];
		for(j = 1; j <= m; j++)
			A[i - j] = (A[i - j] + (ll)q * B[m - j]) % mod;
		A[i] = 0;
	}
}
vector<int> qpow_f(vector<int> A, vector<int> B, int y)
{
	vector<int> ans = {1};
	while(y)
	{
		if(y & 1)
		{
			mul_f(ans, A);
			mod_f(ans, B);
		}
		mul_f(A, A);
		mod_f(A, B);
		y >>= 1;
	}
	return ans;
}
vector<int> gcd_f(vector<int> A, vector<int> B)
{
	int q;
	while(!A.empty())
	{
		B.swap(A);
		if(A.empty())
			break;
		mod_f(A, B);
	}
	q = qpow_p(B.back(), p - 2);
	for(auto &o: B)
		o = (ll)o * q % mod;
	return B;
}
vector<int> facto(vector<int> A) // A.back() = 1
{
	vector<int> ans, B, C;
	int i, j, n, m;
	if(A.size() == 1)
		return {};
	if(A.size() == 2)
		return {(p - A[0]) % p};
	do
	{
		B.resize(A.size() - 1);
		for(auto &o: B)
			o = rng() % p;
		B = qpow_f(B, A, (p - 1) / 2);
		B = gcd_f(A, B);
	}
	while(B.size() == 1 || B.size() == A.size());
	n = (int)A.size() - 1;
	m = (int)B.size() - 1;
	C.resize(n - m + 1);
	for(i = n - m; i >= 0; i--)
	{
		C[i] = A[i + m];
		for(j = 0; j < m; j++)
			A[i + j] = (A[i + j] + (ll)(p - C[i]) * B[j]) % mod;
		A[i + m] = 0;
	}
	ans = facto(B);
	C = facto(C);
	for(auto o: C)
		ans.emplace_back(o);
	return ans;
}
vector<int> fact(vector<int> A)
{
	while(!A.empty() && !A.back())
		A.pop_back();
	if(A.size() <= 1)
		return {};
	vector<int> B = {0, 1};
	B = qpow_f(B, A, p);
	B[1] = (B[1] + p - 1) % p;
	return facto(gcd_f(A, B));
}
bool ntp[N];
int prime[N], cnt;
ll res[N], val[N];
void sieve(int n)
{
	int i, j;
	for(i = 2; i <= n; i++)
	{
		if(!ntp[i])
			prime[++cnt] = i;
		for(j = 1; j <= cnt && i * prime[j] <= n; j++)
		{
			ntp[i * prime[j]] = 1;
			if(!(i % prime[j]))
				break;
		}
	}
}

int main()
{
	// freopen("sqr.in", "r", stdin);
	// freopen("sqr.out", "w", stdout);
	int n, a, b, c, i, j, k, S, ans = 1;
	ll x;
	vector<int> tmp;
	scanf("%d%d%d%d", &a, &b, &c, &n);
	x = (ll)a * n * n + (ll)b * n + c;
	S = pow(x, 1.0 / 3);
	while((ll)(S + 1) * (S + 1) * (S + 1) <= x)
		S++;
	sieve(S);
	for(i = 0; i <= n; i++)
		val[i] = res[i] = (ll)a * i * i + (ll)b * i + c;
	for(i = 1; i <= cnt; i++)
	{
		p = prime[i];
		if(p > S)
		{
			tmp = fact({c % p, b % p, a % p});
			for(auto o: tmp)
				for(k = (o ? o : p); k <= n; k += p)
					while(!(res[k] % p))
					{
						res[k] /= p;
						mp[p]++;
					}
		}
		else
			for(j = 0; j < p; j++)
				if(!(val[j] % p))
					for(k = (j ? j : p); k <= n; k += p)
						while(!(res[k] % p))
						{
							res[k] /= p;
							mp[p]++;
						}
	}
	for(i = 1; i <= n; i++)
		if(res[i] > 1)
		{
			S = sqrt(res[i]);
			while((ll)(S + 1) * (S + 1) <= res[i])
				S++;
			while((ll)S * S > res[i])
				S--;
			if((ll)S * S == res[i])
				mp[S] += 2;
			else
				mp[res[i]]++;
		}
	for(auto o: mp)
		ans = (ll)ans * qpow(o.first % mod, o.second / 2) % mod;
	ans = (ll)ans * ans % mod;
	printf("%d\n", ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 6148kb

input:

17 3 17 14

output:

826280461

result:

wrong answer 1st numbers differ - expected: '123798013', found: '826280461'

Test #2:

score: 5
Accepted
time: 1ms
memory: 6120kb

input:

1 18 0 6

output:

2073600

result:

ok 1 number(s): "2073600"

Test #3:

score: 5
Accepted
time: 1ms
memory: 6004kb

input:

20 6 20 14

output:

315851899

result:

ok 1 number(s): "315851899"

Test #4:

score: 0
Wrong Answer
time: 1ms
memory: 6180kb

input:

81 190 40 125

output:

101501440

result:

wrong answer 1st numbers differ - expected: '924770602', found: '101501440'

Test #5:

score: 0
Wrong Answer
time: 0ms
memory: 6160kb

input:

108 31 110 122

output:

434606290

result:

wrong answer 1st numbers differ - expected: '319532761', found: '434606290'

Test #6:

score: 0
Wrong Answer
time: 1ms
memory: 6188kb

input:

988 194 1412 934

output:

596861172

result:

wrong answer 1st numbers differ - expected: '871618879', found: '596861172'

Test #7:

score: 0
Wrong Answer
time: 1ms
memory: 6272kb

input:

1956 1305 92 1061

output:

122068346

result:

wrong answer 1st numbers differ - expected: '681879967', found: '122068346'

Test #8:

score: 0
Wrong Answer
time: 30ms
memory: 6424kb

input:

75955 165029 149078 5607

output:

181201156

result:

wrong answer 1st numbers differ - expected: '739160804', found: '181201156'

Test #9:

score: 0
Wrong Answer
time: 0ms
memory: 6316kb

input:

3223 4143 3383 4499

output:

879359382

result:

wrong answer 1st numbers differ - expected: '139873119', found: '879359382'

Test #10:

score: 0
Wrong Answer
time: 17ms
memory: 6764kb

input:

9257 3632 1736 9497

output:

941961463

result:

wrong answer 1st numbers differ - expected: '158679485', found: '941961463'

Test #11:

score: 0
Wrong Answer
time: 39ms
memory: 7300kb

input:

13657 8517 9780 16829

output:

282771930

result:

wrong answer 1st numbers differ - expected: '499148359', found: '282771930'

Test #12:

score: 0
Wrong Answer
time: 78ms
memory: 6780kb

input:

152794 105986 145639 8507

output:

35205059

result:

wrong answer 1st numbers differ - expected: '432311896', found: '35205059'

Test #13:

score: 0
Wrong Answer
time: 1ms
memory: 6200kb

input:

2209 13866 42748 227

output:

559284532

result:

wrong answer 1st numbers differ - expected: '576767941', found: '559284532'

Test #14:

score: 0
Wrong Answer
time: 2ms
memory: 6312kb

input:

14279 7290 25915 2265

output:

740972447

result:

wrong answer 1st numbers differ - expected: '173729704', found: '740972447'

Test #15:

score: 0
Wrong Answer
time: 107ms
memory: 8136kb

input:

24703 6569 26356 26534

output:

227669872

result:

wrong answer 1st numbers differ - expected: '108700579', found: '227669872'

Test #16:

score: 0
Wrong Answer
time: 163ms
memory: 7044kb

input:

187348 185285 76358 13718

output:

592933629

result:

wrong answer 1st numbers differ - expected: '425402711', found: '592933629'

Test #17:

score: 0
Wrong Answer
time: 164ms
memory: 7408kb

input:

189507 133399 143282 13930

output:

859319458

result:

wrong answer 1st numbers differ - expected: '608644351', found: '859319458'

Test #18:

score: 0
Wrong Answer
time: 2033ms
memory: 17148kb

input:

102698 162019 98595 137546

output:

729119379

result:

wrong answer 1st numbers differ - expected: '388084925', found: '729119379'

Test #19:

score: 0
Wrong Answer
time: 161ms
memory: 7332kb

input:

152381 90362 151048 15578

output:

923172002

result:

wrong answer 1st numbers differ - expected: '413943175', found: '923172002'

Test #20:

score: 0
Wrong Answer
time: 396ms
memory: 14276kb

input:

34402 49259 74598 64198

output:

293176160

result:

wrong answer 1st numbers differ - expected: '733372582', found: '293176160'