NBUT1223-Friends number

本文介绍了一种用于找出特定区间内所有友好数对(即两个数互为对方除自身外所有因子之和)的算法实现。通过预计算友好数对并存储在一个数组中,可以在给定范围内快速检索到所有匹配的数对。

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

  • Friends number

  • 时间限制: 1000 ms 内存限制: 131072 K
  • 问题描述
  • 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

题意:若一个数的除它自己以外的所有因子和为sum,且sum除它自己以外的所有因子和为这个数,那么这对数是友好的,问【x,y】中有几对这样的数

解题思路:打表打出所有这样的数,然后暴力查找


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <functional>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

int a[100]={220,1184,2620,5020,6232,10744,12285,17296,63020,66928,67095,69615,79750,
100485,122265,122368,141664,142310,171856,176272,185368,196724,280540,308620,319550,
356408,437456,469028,503056,522405,600392,609928,624184,635624,643336,667964,726104,
802725,879712,898216,947835,998104,1077890,1154450,1156870,1175265,1185376,1280565,
1328470,1358595,1392368,1466150,1468324,1511930,1669910,1798875,2082464,2236570,2652728,
2723792,2728726,2739704,2802416,2803580,3276856,3606850,3786904,3805264,4238984,4246130,
4259750,4482765,4532710,4604776};
int b[100]={284,1210,2924,5564,6368,10856,14595,18416,76084,66992,71145,87633,88730,124155,
139815,123152,153176,168730,176336,180848,203432,202444,365084,389924,430402,399592,455344,
486178,514736,525915,669688,686072,691256,712216,652664,783556,796696,863835,901424,980984,
1125765,1043096,1099390,1189150,1292570,1438983,1286744,1340235,1483850,1486845,1464592,1747930,
1749212,1598470,2062570,1870245,2090656,2429030,2941672,2874064,3077354,2928136,2947216,3716164,
3721544,3892670,4300136,4006736,4314616,4488910,4445050,5120595,6135962,5162744};

int main()
{
    int x,y;
    while(~scanf("%d%d",&x,&y))
    {
        int cnt=0;
        for(int i=0;i<74;i++)
        {
            if(a[i]>=x&&b[i]<=y) cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值