HDU 6211 Pythagoras (预处理, 2017 ACM/ICPC Asia Regional Qingdao Online)

本文探讨了在特定约束条件下,高效寻找并计算勾股数的方法。利用三叉树结构和矩阵运算,提出了一种递归搜索算法,该算法能够在预处理的帮助下满足时间限制要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem

Given a list of integers a0,a1,a2,,a2k1 . Pythagoras triples over 109 are all solutions of x2+y2=z2 where x,y and z are constrained to be positive integers less than or equal to 109 . You are to compute the sum of aymod2k of triples (x,y,z) such that x

Idea

首先,刨除对 aymod2k 的计算。此题首先要求求有多少组 (x, y, z) 满足 gcd(x, y) = gcd(x, z) = gcd(y, z) = gcd(x, y, z) = 1 x,y,z109 。上述描述即求有多少组本原勾股数 或 素勾股数 (x, y, z) 满足 x,y,z109

在求本原勾股数已有成熟 O(N2) 的算法,但此处无法使用 N109

同时所有本原勾股数可以构成一个完整的三叉树形式 Tree of primitive Pythagorean triples

它们分别通过矩阵

ABC=122212223=122212223=122212223

得到。 详见 Tree of primitive Pythagorean triples

由于在 x,y,z109 时,最大的树深度为 20000 ,故可用递归的形式深搜。

总共产生的本原勾股数组数在 1.5×108 左右,在预处理的情况下可以勉强达到时限要求。

之后的本题具体内容的处理不做具体说明(最难的问题已经解决了)。

Code

#include<bits/stdc++.h>
using namespace std;
const int LMT = 1e9;
long long cnt = 0;
const int N = 1<<17;
const int MOD = N - 1;
int dig[N], a[N], T, k;
void solve(long long a, long long b, long long c)
{
    if(c > LMT) return;
    dig[ max(a, b)&MOD ] ++;
    long long aa = a<<1;
    long long bb = b<<1;
    long long cc = c<<1;
    solve(a-bb+cc, aa-b+cc, aa-bb+cc+c);
    //solve(a-(b<<1)+(c<<1), (a<<1)-b+(c<<1), (a<<1)-(b<<1)+(c<<1)+c);

    solve(a+bb+cc, aa+b+cc, aa+bb+cc+c);
    //solve(a+(b<<1)+(c<<1), (a<<1)+b+(c<<1), (a<<1)+(b<<1)+(c<<1)+c);

    solve(bb+cc-a, b+cc-aa, bb+cc+c-aa);
    //solve(-a+(b<<1)+(c<<1), -(a<<1)+b+(c<<1), -(a<<1)+(b<<1)+(c<<1)+c);
}
int main()
{
    solve(3, 4, 5);
    scanf("%d", &T);
    while(T-- && scanf("%d", &k)!=EOF)
    {
        int mod = (1<<k);
        for(int i=0;i<mod;i++)
            scanf("%d", &a[i]);
        long long ans = 0;
        for(int i=0, j=0;i<N;i++,j++)
        {
            if(j == mod)    j = 0;
            ans += dig[i] * a[j];   
        }
        printf("%lld\n", ans);
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值