NYOJ 411 Friends number (数论--因子和)

来源:互联网 发布:淘宝宝贝标题范文 编辑:程序博客网 时间:2024/05/19 13:29

链接:点击打开链接

题意:

Friends number

时间限制:2000 ms  |  内存限制:65535 KB
难度:2

描述
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 1001 1000
样例输出
01
提示
6 is a number whose sum of all divisors is 6. 6 is not a friend number, these number is called Perfect Number.
来源
辽宁省10年省赛
思路:其实理解题意就比较简单,求区间满足“friend num”的数的对子的个数,friend num:假设,a,b,如果a的所有因子和==b,且b的所有因子和==a,,及满足!

oj数据有点大,常规方法,一个劲的超时,看了一下排在前几名的人的代码,基本都是打表过,(囧~~),后来有点提示,用数组模拟过了,其实还可以用容器

,那样写的话看的比较舒服。

代码:(红色区域重点)

 #include <stdio.h>#include <iostream>#include <string.h>#include <algorithm>using namespace std;#define N 5000001#define CLR(arr, what) memset(arr, what, sizeof(arr))int a[N] ,pre[N],last[N],ss=0;void getsum();int main(){    //getsum();//    for(int i = 2; i <= N; i++) a[i] = 1;//    for(int i = 2; i * i <= N; i++)//        for (int j = i; j * i <= N; j++)//            a[i * j] += i + j;   <span style="color:#ff0000;"> for(int i = 2; i <= N; i++) a[i] = 1;    for(int i = 2; i * i <= N; i++)        for(int j = i + 1; i * j <= N; j++)            a[i * j] += i + j;    for(int i = 2; i * i <= N; i++)        a[i * i] += i;    for(int i=1; i<=N; i++)    {        int t=a[i];        if (t > i && t <=N&& a[t] == i)        {            pre[ss]=i;            last[ss]=t;            ss++;        }    }</span>    int m,i,j,n;    while(~scanf("%d%d",&m,&n))    {        int count = 0;        for(i=0; i<ss; i++)            if(pre[i]>=m&&last[i]<=n)                count++;        printf("%d\n",count);    }    return 0;}        

When you want to give up, think of why you persist until now!



0 0
原创粉丝点击