NBUT 1223 Friends number(打表)

本文介绍了一种寻找特定范围内友好数对数量的方法,并提供了一个示例性的C语言实现。友好数是指两个数中一个数的所有真因数之和等于另一个数。文章通过预处理所有可能的友好数对,实现了快速查询给定区间内的友好数对数目。

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

Friends number

问题描述
Paula and Tai are couple. There are many stories between them. The day Paula left by airplane, Tai send one message to telephone 2200284, then, everything is changing… (The story in “the snow queen”).

After a long time, Tai tells Paula, the number 220 and 284 is a couple of friends number, as they are special, all divisors of 220’s sum is 284, and all divisors of 284’s sum is 220. Can you find out there are how many couples of friends number less than 10,000. Then, how about 100,000, 200,000 and so on.

The task for you is to find out there are how many couples of friends number in given closed interval [a,b]。

输入
There are several cases.
Each test case contains two positive integers a, b(1<= a <= b <=5,000,000).
Proceed to the end of file.
输出
For each test case, output the number of couples in the given range. The output of one test case occupied exactly one line.
样例输入
1 100
1 1000
样例输出
0
1
提示
6 is a number whose sum of all divisors is 6. 6 is not a friend number, these number is called Perfect Number.
来源
辽宁省赛2010

ps:打表即可

代码:

#include<stdio.h>
#define maxn 5000000
struct node
{
    int x,y;
}q[100000];
int a[maxn+5],flag[maxn+5];
int ps;

void init()
{
    a[1]=1;
    for(int i=1;i<=maxn;i++)
    {
        for(int j=i+i;j<=maxn;j+=i)
            a[j]+=i;
    }
    ps=0;
    for(int i=1;i<maxn;i++)
    {
        if(a[i]>maxn||flag[i])
            continue;
        int k=a[i];
        if(a[k]==i&&i!=k)
            flag[k]=1,flag[i]=1,q[ps].x=i,q[ps++].y=k;
    }
}

int solve(int le,int ri)
{
    int ans=0;
    for(int i=0;i<ps;i++)
    {
        if(q[i].x>=le&&q[i].y<=ri)
            ans++;
    }
    return ans;
}

int main()
{
    init();
    int le,ri;
    while(~scanf("%d%d",&le,&ri))
    {
        printf("%d\n",solve(le,ri));
    }
    return 0;
}



ps:总共就71对,也可以直接打表存起来。。

总结:以前以为打表只是找规律打表,结果比赛的时候一看这题数据有点大写的话肯定超时,完全没想到打表。。。。
还是刷题少啊,这是经典的空间换时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值