NEU 1008 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 <=500,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.


原先设计了个算法。交上去2000多MS。。超时了。。后来输出全部数据。。发现答案最多只有28.。。打表。


这是我用于打表的代码

/*
 * @user ipqhjjybj
 * @Time
 * @data 20130629
 */
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>

#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
using namespace std;

#define inf 0x3f3f3f3f
#define MAXN 500010
#define clr(x,k) memset((x),(k),sizeof(x))
#define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define Base 10000

typedef vector<int> vi;

#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

int num[500020];

void getSumDivider(int n){
    int k,i,j;
    for(i = 0;i <= n;i++) num[i]=0;
    for(i = 2;i <= n;i++) num[i]+=1;
    for(i = 2;i+i <= n;i++){
        for(j = i+i,k=2;j <= n;j+=i,k++){
            num[j]+=k;
        }
    }
}
int main(){
    freopen("1008.out","w",stdout);
    int a,b,aaa,bbb,i,ans;
    getSumDivider(MAXN);
    //while(scanf("%d %d",&a,&b)!=EOF){
        a = 1;b = 500000;
        ans=0;
        for(i = a;i <= b;i++){
            aaa = num[i];
            if(aaa>b)continue;
            bbb = num[aaa];
            if(bbb == i && a <= aaa && aaa <= b && aaa != bbb)
            {
                printf("%d,%d,\n",i,aaa);
                ans++;
            }
        }
        printf("%d\n",ans/2);
    //}
    return 0;
}

这是我最终AC 的代码:

/*
 * @user ipqhjjybj
 * @Time
 * @data 20130629
 */
#include <stdio.h>
#include <stdlib.h>
int Ans[56][2]={
    220,284,
284,220,
1184,1210,
1210,1184,
2620,2924,
2924,2620,
5020,5564,
5564,5020,
6232,6368,
6368,6232,
10744,10856,
10856,10744,
12285,14595,
14595,12285,
17296,18416,
18416,17296,
63020,76084,
66928,66992,
66992,66928,
67095,71145,
69615,87633,
71145,67095,
76084,63020,
79750,88730,
87633,69615,
88730,79750,
100485,124155,
122265,139815,
122368,123152,
123152,122368,
124155,100485,
139815,122265,
141664,153176,
142310,168730,
153176,141664,
168730,142310,
171856,176336,
176272,180848,
176336,171856,
180848,176272,
185368,203432,
196724,202444,
202444,196724,
203432,185368,
280540,365084,
308620,389924,
319550,430402,
356408,399592,
365084,280540,
389924,308620,
399592,356408,
430402,319550,
437456,455344,
455344,437456,
469028,486178,
486178,469028
};
int main(){
    int a,b,ans,i;
    while(scanf("%d %d",&a,&b)!=EOF){
        ans=0;
        for(i=0;i<56;i++)
            if(a<=Ans[i][0]&&Ans[i][0]<Ans[i][1]&&Ans[i][1]<=b)
                ans++;
        printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值