跳到正文

题解

题解:P12312 [蓝桥杯 2024 国 C] 六一儿童节

#SUSPENDED#约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年6月10日
收录
2025年12月19日
更新
2026年9月21日
篇幅
约 3 分钟
题目
P12312

版本

  1. v1当前

P12312 [蓝桥杯 2024 国 C] 六一儿童节

题目简介:

从 11 到 2024060120240601 中找 xx 和 yy 两个数,使 xx 小于 yy,并且 xx 的平方加上 yy 的平方能被 64216421 整除。

思路分析:

最简单的方法,暴力枚举,二重循环,单出来结果要好久,所以启用,但也行。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int ans = 0;
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	for (int i = 1; i <= 20240601; i++) {
		for (int j = 1; j <= 20240601; j++) {
			if ((i * i + j * j) / 6421 == 0) {
				ans ++;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

然后思考正解。

根据欧拉定理和中国剩余定理,可以发现 nn 的平方模 64216421 的值由 nn 模 64216421 和 nn 模 64206420 来决定,也就是说最大周期为 6421×62406421\times 6240,也就是 3894384038943840,直接遍历即可,用快速幂加快速度。

代码:

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int p = 6421;
int ul[20240605],un[6425],ans;
int f(int a, int b) {//快速幂
    int sum = 1;
    a = a % p;
    while (b > 0) {
        if(b & 1) {
			sum = (sum * a) % p;
		}
        a = (a * a) % p;
        b = b >> 1;
    }
    return sum % p;
}
signed main() {
	for(int i = 1; i <= 20240601; i++) {
		ul[i] = f(i,i);
	}
	for(int i = 1; i <= 20240601; i++) {
		ans += un[(6421-ul[i]) % p];
		++ un[ul[i]];
	}
	cout << ans;
	return 0;
}

因为只要答案,所以超时没事。

最终答案 5134914110751349141107。

谢谢观看,求过。

原文
存档评论3 条抓取于