1 solutions

  • 0
    @ 2026-4-14 8:33:07

    我们都知道,勾股方程的本解都可以表达成

    $$a = k \times (m^2 - n^2),b = k \times 2nm,c = k \times (m^2 + n^2)$$

    其中 m>n>0m > n > 0,gcd⁡(m,n)=1\gcd(m, n) = 1,且 mm 和 nn 奇偶性不同。此时我们已经知道了 cc 的值,所以我们现在只需要枚举所有的 kk,接着只需要找出所有平方出超过 ck\frac{c}{k} 的数,接着看剩余的数是否也是平方数,是就将答案加二,应为 a,ba,b 可以交换,接着将对应的 aa 异或进去即可。

    #include<bits/stdc++.h>
    #define ll long long
    #define ull unsigned long long
    using namespace std;
    const ll N = 2e5 + 5;
    const ll M = 1e5 + 5;
    const ll INF = 1e18;
    const ll MOD = 1e9 + 7;
    ll n,cnt,ans;
    ll gcd(ll x,ll y){
    	return (y == 0) ? x : gcd(y,x % y); //最大公因数
    }
    void getans(ll x,ll y){
    	for(ll a = 1;a * a < x;a++){ //枚举第一个数
    		ll a1 = a * a,b1 = x - a1,b = sqrt(b1); //求出第二个数
    		if(b * b != b1 || a > b) continue; //只看a > b的,应为a <= b会在另一半看到
    		if(gcd(a,b) == 1 && a % 2 != b % 2){ //根据本解的条件判断
    			cnt++; //统计答案
    			ans ^= (y * (b1 - a1)) ^ (y * (2 * a * b)); //将两个数异或进去
    		}
    	}
    }
    void work(){
    	cin >> n;
    	for(ll i = 1;i * i <= n;i++){
    		if(n % i) continue;
    		if(i * i == n){
    			getans(i,n / i); //平方数特判
    		}else{
    			getans(n / i,i); //将枚举到的和另一半进行计算
    			getans(i,n / i); //交换也计算
    		}
    	}
    	cout << cnt * 2 << "\n" << ans;
    }
    int main(){
    	ios::sync_with_stdio(false);
    	cin.tie(nullptr);cout.tie(nullptr);
    	//freopen("theorem.in","r",stdin);
    	//freopen("theorem.out","w",stdout);
    	int oT_To = 1;
    	//cin >> oT_To;
    	while(oT_To--){ work();}
    	return 0;
    }
    
    • 1

    Information

    ID
    93
    Time
    2000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    (None)
    # Submissions
    34
    Accepted
    7
    Uploaded By