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

· · 题解

思路分析:

以知 x^x+y^y\equiv0。

可以转化为,y^y\equiv-x^x。

所以,对于每一个 x,我们需要找到 y>x 使得以上等式成立。

由于 6421 是一个质数,我们可以利用模运算的性质,φ(6421)=6420。

因为 x 的周期是 6421,x 的周期是 6420,所以最大周期为 38943840,大于 20240601,所以直接遍历就行了。

代码步骤如下。

#include<bits/stdc++.h>
#define LL long long 
using namespace std;
LL f(LL a,LL b){ 
    LL sum=1;
    while(b){
        if(b&1){
            sum=(sum*a)%6421;
        }
        a=a*a%6421;
        b/=2;
    }
    return sum;
}
LL a[2000001];
int main(){
    LL sum=0;
    for(LL i=1;i<=20240601;i++){ 
        if(i%6421==0){
            a[0]++;
        }
        else {
            a[f(i%6421,i%6420)]++;
        }
    }
    for(LL i=0;i<6421;i++){ 
        if(i<(6421-i)%6421){
            sum+=a[i]*a[(6421-i)%6421];
        }
        else if (i==(6421-i)%6421){
            sum+=a[i]*(a[i]-1)/2;
        }
    }
    cout<<sum<<endl;
    return 0;
}

输出答案代码。

#include<bits/stdc++.h>
using namespace std;
int main(){
    cout<<"51349141107"<<endl;
    return 0;
}

打完后得到答案为 51349141107。