NBUT - 1223 Friends number (暴力打表)

这是一个关于寻找朋友数对的问题,朋友数是指两个数的因数之和相等的数对。给定一个范围[a, b],任务是找到这个区间内的朋友数对数量。通过暴力打表的方法,使用素数筛法的思路来计算每个数的因数和,从而找出所有在[1, 5,000,000]范围内的朋友数对,并解决对于不同范围[a, b]的情况。" 48791969,5520233,Mac上配置iOS开发环境:Ice与Xcode集成,"['ios开发', 'xcode', '集成开发环境']
摘要由CSDN通过智能技术生成

 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]。

 

 

 

Input

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.

Output

For each test case, output the number of couples in the given range. The output of one test case occupied exactly one line. 

Sample Input

1 100
1 1000

Sample Output

0
1

Hint

6 is a number whose sum of all divisors is 6. 6 is not a friend number, these number is called Perfect Number.

 

题目链接:https://ac.2333.moe/Problem/view.xhtml?id=1223

题目大意: 两个数x与y  如果x所有因数的和等于y 且 y所有因数的和等于x (x!=y) 就称这对数为 Friends number

求 [a ,b ]有多少对 Friends number

思路:因为b的最大值为5000000不算大,可以打表求出1~5000000间的所有 Friends number

用素数筛法的思路打表 i 的因数和,然后找出所有的 Friends number 存到数组中

当然打表的过程不能直接提交,肯定会超时的

代码:
 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=5e6;
int a[N+5];
int b[N]={284,220,220,284,1210,1184,1184,1210,2924,2620,2620,2924,5564,5020,5020,5564,6368,6232,6232,6368,10856,10744,10744,10856,14595,12285,12285,14595,18416,17296,17296,18416,76084,63020,66992,66928,66928,66992,71145,67095,87633,69615,67095,71145,63020,76084,88730,79750,69615,87633,79750,88730,124155,100485,139815,122265,123152,122368,122368,123152,100485,124155,122265,139815,153176,141664,168730,142310,141664,153176,142310,168730,176336,171856,180848,176272,171856,176336,176272,180848,203432,185368,202444,196724,196724,202444,185368,203432,365084,280540,389924,308620,430402,319550,399592,356408,280540,365084,308620,389924,356408,399592,319550,430402,455344,437456,437456,455344,486178,469028,469028,486178,514736,503056,503056,514736,525915,522405,522405,525915,669688,600392,686072,609928,691256,624184,712216,635624,652664,643336,643336,652664,783556,667964,600392,669688,609928,686072,624184,691256,635624,712216,796696,726104,667964,783556,726104,796696,863835,802725,802725,863835,901424,879712,980984,898216,879712,901424,1125765,947835,898216,980984,1043096,998104,998104,1043096,1099390,1077890,1077890,1099390,947835,1125765,1189150,1154450,1292570,1156870,1438983,1175265,1286744,1185376,1154450,1189150,1340235,1280565,1185376,1286744,1156870,1292570,1483850,1328470,1280565,1340235,1486845,1358595,1464592,1392368,1175265,1438983,1392368,1464592,1747930,1466150,1749212,1468324,1328470,1483850,1358595,1486845,1598470,1511930,1511930,1598470,2062570,1669910,1466150,1747930,1468324,1749212,1870245,1798875,1798875,1870245,1669910,2062570,2090656,2082464,2082464,2090656,2429030,2236570,2236570,2429030,2941672,2652728,2874064,2723792,3077354,2728726,2928136,2739704,2947216,2802416,3716164,2803580,2723792,2874064,2739704,2928136,2652728,2941672,2802416,2947216,2728726,3077354,3721544,3276856,3892670,3606850,2803580,3716164,3276856,3721544,4300136,3786904,4006736,3805264,3606850,3892670,3805264,4006736,4314616,4238984,4488910,4246130,4445050,4259750,3786904,4300136,4238984,4314616,4259750,4445050,4246130,4488910};
int cnt;
//void init()  //打表
//{
//    a[0]=a[1]=1;
//    for(int i=2;i<=N;i++)
//    {
//        a[i]++;
//        for(int j=i+i;j<=N;j+=i)
//            a[j]+=i;
//    }
//    cnt=0;
//    for(int i=2;i<=N;i++)
//    {
//        int tmp=a[i];
//        if(a[tmp]==i&&i!=tmp)
//        {
//            cnt++;
//            printf("%d %d\n",i,tmp);
//        }
//    }
//    printf("%d\n",cnt);
//}
int main()
{
//    init();
    int l,r;
    while(~scanf("%d%d",&l,&r))
    {
        int ans=0;
        for(int i=0;i<284;i+=2)
        if(b[i]>=l&&b[i]<=r&&b[i+1]<=r&&b[i+1]>=l)
            ans++;
        printf("%d\n",ans/2);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值