2022namo div1 平方计数 (数论)

题目

http://oj.daimayuan.top/course/10/problem/607
在这里插入图片描述

分析

a^2+b=c^2
b	=(c+a)(c-a)
 	=(x+2a)(x)

对每个数b,枚举约束 x,检查是否存在对应的a即可。

在这里插入图片描述
一百万以内的数,最大约数个数是240。

代码

大常数选手

// http://oj.daimayuan.top/course/10/problem/607
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;

int n;
int a[MAXN];
int cnt[MAXN];
int used[MAXN];
int P[200]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 
67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 
139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 
223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 
293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 
383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 
463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 
569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 
647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 
743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 
839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 
941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 
1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 
1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 
1187, 1193, 1201, 1213, 1217, 1223};
long long ans;
int tmpp[200];

void rd(int &X)
{
    X=0;
    char ch=getchar();
    while (ch<'0' || ch>'9') ch=getchar();
    while (ch>='0' && ch<='9') X=X*10+ch-'0', ch=getchar();
}

int A;
void dfs(int x, int p, int flag)
{
    int y=A/x;
    if (y>x) return;
    if (flag && (x-y)%2==0) {
        //printf("%d\n",x);
        ans+=cnt[A]*cnt[(x-y)/2];
    }

    if (p<0) return;
    dfs(x, p-1, 0);
    while (x%tmpp[p]==0) {
        x/=tmpp[p];
        dfs(x, p-1, 1);
    }
}


void solve()
{
    rd(n);
    for (int i=1; i<=n; i++) {
        rd(a[i]);
        cnt[a[i]]++;
    }
    for (int i=1, ta; i<=n; i++) if (!used[a[i]]) {
        used[a[i]]=1;
        A=ta=a[i];
        int top=-1;
        for (int i=0; P[i]*P[i]<=ta; i++) if (ta%P[i]==0) {
            tmpp[++top]=P[i];
            while (ta%P[i]==0) ta/=P[i];
        }
        if (ta>1) tmpp[++top]=ta;
        dfs(a[i], top, 1);
        //puts("");
    }

    printf("%lld\n",ans);
}


int main()
{
    solve();
    return 0;
}




/*
        int k=1, t=a[i], x1, x2;
        while (k<t) {
            if (t%k==0) {
                x1=k;
                x2=B/k;
                if (x2<x1) break;
                if ((x2-x1)&1) continue;
                ans += cnt[(x2-x1)/2];
            }
            k=t/(t/(k+1));
        }


    for (int i=1; i<=n; i++) if (!used[a[i]]) {
        used[a[i]]=1;

        int B=a[i];
        int L=1, R=B, x1, x2;
        while (L<R) {
            if (B%L == 0) {
                x1=L;
                x2=B/L;
                if (x2<x1) break;
                if ((x2-x1)%2==0) ans += cnt[(x2-x1)/2];
            }
            R=B/(L+1);
            L=B/R;
        }
        if (L==R && n%L==0) ans+=cnt[0];
    }
*/

这个约数枚举好像是假的,有毒

// http://oj.daimayuan.top/course/10/problem/607
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;

int n;
int a[MAXN];
int cnt[MAXN];
int used[MAXN];




void solve()
{
    long long ans=0;
    scanf("%d",&n);
    for (int i=1; i<=n; i++) {
        scanf("%d",&a[i]);
        cnt[a[i]]++;
    }
    for (int i=1; i<=n; i++) if (!used[a[i]]) {
        used[a[i]]=1;

        int B=a[i];
        int k=1, t=a[i], x1, x2;
        while (k<t) {   // 枚举约数
            if (t%k==0) {
                x1=k;
                x2=B/k;
                if ((x2-x1)%2==0) ans += cnt[(x2-x1)/2];
            }
            k=t/(t/(k+1));
        }
        if (k==t) ans+= cnt[0];
    }

    printf("%lld\n",ans);
}


int main()
{
    solve();
    return 0;
}




/*
        int k=1, t=a[i], x1, x2;
        while (k<t) {
            if (t%k==0) {
                x1=k;
                x2=B/k;
                if (x2<x1) break;
                if ((x2-x1)&1) continue;
                ans += cnt[(x2-x1)/2];
            }
            k=t/(t/(k+1));
        }


    for (int i=1; i<=n; i++) if (!used[a[i]]) {
        used[a[i]]=1;

        int B=a[i];
        int L=1, R=B, x1, x2;
        while (L<R) {
            if (B%L == 0) {
                x1=L;
                x2=B/L;
                if (x2<x1) break;
                if ((x2-x1)%2==0) ans += cnt[(x2-x1)/2];
            }
            R=B/(L+1);
            L=B/R;
        }
        if (L==R && n%L==0) ans+=cnt[0];
    }
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值