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

来源:互联网 发布:端口号的目的是 编辑:程序博客网 时间:2024/06/05 02:52

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) = 1x,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);    }}
阅读全文
0 0
原创粉丝点击