NBUT [1223] Friends number

  • [1223] 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

题意:背景是一个电影故事,当时一直在迷惑2200284,220和284中间为什么要加一个0?;
           本题是要 求给一个区间,问给定区间内得friend number的数目;
           friend number就是若一个数A的因子和为B,那么B的因子和为A,那么这个数字就是friend number;

思路:就是打表的方法,因为数据太大了,那么你得会打表才可以,如果你不会打表,那么也很难解决,那么怎么
           求得各数得因子和呢,模仿得就是素数打表;

代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn=5000006;

int c1[]={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};
int c2[]={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};

int main()
{
    int a,b;
    while(~scanf("%d%d",&a,&b))
    {
        int ans=0;
        for(int i=0;i<72;i++)
        {
            if(c1[i]>=a&&c2[i]<=b)
                ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

打表代码:

//和素数打表得思路相同;
int main()
{  
    memset(a,0,sizeof(a));
    a[0]=a[1]=1;
    for(int i=2;i<=maxn;i++)
    {
        a[i]++;
        for(int j=2*i;j<=maxn;j+=i)
            a[j]+=i;
    }
    int cnt=0;
    for(int i=2;i<=maxn;i++)
    {
        if(a[i]<maxn&&i==a[a[i]]&&i<a[i])//如果a[i]>maxn,就爆栈了;
        {
            cnt++;
//            printf("%d %d\n",i,a[i]);
//              printf("%d,",i);
              printf("%d,",a[i]);
        }
    }
    printf("%d\n",cnt);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值