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
  • 操作

 

这个题目真心好极了,看了一个上午了,用了很多种办法进行打表,都不能打出来,终于再问了山理工的大神之后学会了正确的

打表办法,原理非常类似于素数打表,真的好牛啊,我这种小菜真是佩服极了

 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <time.h>
#include <queue>
#include <cstring>
#include <algorithm>
/*
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int ans[5000010];
int main()
{

    ans[0] = ans[1] = 1;

    memset(ans,0,sizeof(ans));
    int i,j;
    for(i = 2; i <= 5000000; ++i)
    {
        ++ans[i];
        for(j = i+i; j <= 5000000; j += i)
        {
            ans[j] += i;
        }
    }
    //for(i=2;i<=1000;i++)
    //printf("ans %d \n",ans[i]);
    int sum = 0;

    for(i = 2; i <= 5000000; ++i)
    {
        if(ans[i] <= 5000000 && i == ans[ans[i]] && i != ans[i])
        {    
            cout<<min(i,ans[i])<<","<<endl;

        }
    }
    //cout<<"asdfasdfasdfasdfasdfasdf\n";

    for(i = 2; i <= 5000000; ++i)
    {
        if(ans[i] <= 5000000 && i == ans[ans[i]] && i != ans[i])
        {
            cout<<max(i,ans[i])<<","<<endl;
        }
    }
}











*/
using namespace std;

int l[500] = {220,
              220,
              1184,
              1184,
              2620,
              2620,
              5020,
              5020,
              6232,
              6232,
              10744,
              10744,
              12285,
              12285,
              17296,
              17296,
              63020,
              66928,
              66928,
              67095,
              69615,
              67095,
              63020,
              79750,
              69615,
              79750,
              100485,
              122265,
              122368,
              122368,
              100485,
              122265,
              141664,
              142310,
              141664,
              142310,
              171856,
              176272,
              171856,
              176272,
              185368,
              196724,
              196724,
              185368,
              280540,
              308620,
              319550,
              356408,
              280540,
              308620,
              356408,
              319550,
              437456,
              437456,
              469028,
              469028,
              503056,
              503056,
              522405,
              522405,
              600392,
              609928,
              624184,
              635624,
              643336,
              643336,
              667964,
              600392,
              609928,
              624184,
              635624,
              726104,
              667964,
              726104,
              802725,
              802725,
              879712,
              898216,
              879712,
              947835,
              898216,
              998104,
              998104,
              1077890,
              1077890,
              947835,
              1154450,
              1156870,
              1175265,
              1185376,
              1154450,
              1280565,
              1185376,
              1156870,
              1328470,
              1280565,
              1358595,
              1392368,
              1175265,
              1392368,
              1466150,
              1468324,
              1328470,
              1358595,
              1511930,
              1511930,
              1669910,
              1466150,
              1468324,
              1798875,
              1798875,
              1669910,
              2082464,
              2082464,
              2236570,
              2236570,
              2652728,
              2723792,
              2728726,
              2739704,
              2802416,
              2803580,
              2723792,
              2739704,
              2652728,
              2802416,
              2728726,
              3276856,
              3606850,
              2803580,
              3276856,
              3786904,
              3805264,
              3606850,
              3805264,
              4238984,
              4246130,
              4259750,
              3786904,
              4238984,
              4259750,
              4246130,
              -1
             };

int r[500] =
{
    284,
    284,
    1210,
    1210,
    2924,
    2924,
    5564,
    5564,
    6368,
    6368,
    10856,
    10856,
    14595,
    14595,
    18416,
    18416,
    76084,
    66992,
    66992,
    71145,
    87633,
    71145,
    76084,
    88730,
    87633,
    88730,
    124155,
    139815,
    123152,
    123152,
    124155,
    139815,
    153176,
    168730,
    153176,
    168730,
    176336,
    180848,
    176336,
    180848,
    203432,
    202444,
    202444,
    203432,
    365084,
    389924,
    430402,
    399592,
    365084,
    389924,
    399592,
    430402,
    455344,
    455344,
    486178,
    486178,
    514736,
    514736,
    525915,
    525915,
    669688,
    686072,
    691256,
    712216,
    652664,
    652664,
    783556,
    669688,
    686072,
    691256,
    712216,
    796696,
    783556,
    796696,
    863835,
    863835,
    901424,
    980984,
    901424,
    1125765,
    980984,
    1043096,
    1043096,
    1099390,
    1099390,
    1125765,
    1189150,
    1292570,
    1438983,
    1286744,
    1189150,
    1340235,
    1286744,
    1292570,
    1483850,
    1340235,
    1486845,
    1464592,
    1438983,
    1464592,
    1747930,
    1749212,
    1483850,
    1486845,
    1598470,
    1598470,
    2062570,
    1747930,
    1749212,
    1870245,
    1870245,
    2062570,
    2090656,
    2090656,
    2429030,
    2429030,
    2941672,
    2874064,
    3077354,
    2928136,
    2947216,
    3716164,
    2874064,
    2928136,
    2941672,
    2947216,
    3077354,
    3721544,
    3892670,
    3716164,
    3721544,
    4300136,
    4006736,
    3892670,
    4006736,
    4314616,
    4488910,
    4445050,
    4300136,
    4314616,
    4445050,
    4488910,
    -1
};

struct N
{
    int x,y;
} st[11000];

bool cmp(N s1,N s2)
{
    return s1.x < s2.x;
}
int main()
{
    int i,L,R,ans;

    for(i = 0; l[i] != -1; ++i)
    {
        st[i].x = l[i];
        st[i].y = r[i];
    }

    int Top = i;

    sort(st,st+Top,cmp);


        while(scanf("%d %d",&L,&R) != EOF)
        {
            ans = 0;

            for(i = 0; i < Top; ++i)
            {
                if(L <= st[i].x && st[i].y <= R && st[i].x != st[i-1].x && st[i].y != st[i-1].y)
                {
                    ans++;
                }
            }

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

    

    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值