Friends number 数论 打表

来源:互联网 发布:vr相机软件 编辑:程序博客网 时间:2024/05/03 01:40

题目描述:

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.。。打表。


这是我用于打表的代码

[cpp] view plaincopy
  1. /* 
  2.  * @user ipqhjjybj 
  3.  * @Time 
  4.  * @data 20130629 
  5.  */  
  6. #include <cstdio>  
  7. #include <cmath>  
  8. #include <cstdlib>  
  9. #include <ctime>  
  10.   
  11. #include <iostream>  
  12. #include <cmath>  
  13. #include <algorithm>  
  14. #include <numeric>  
  15. #include <utility>  
  16.   
  17. #include <cstring>  
  18. #include <vector>  
  19. #include <stack>  
  20. #include <queue>  
  21. #include <map>  
  22. #include <string>  
  23. using namespace std;  
  24.   
  25. #define inf 0x3f3f3f3f  
  26. #define MAXN 500010  
  27. #define clr(x,k) memset((x),(k),sizeof(x))  
  28. #define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))  
  29. #define cpy(x,k) memcpy((x),(k),sizeof(x))  
  30. #define Base 10000  
  31.   
  32. typedef vector<int> vi;  
  33.   
  34. #define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)  
  35.   
  36. #define max(a,b) ((a)>(b)?(a):(b))  
  37. #define min(a,b) ((a)<(b)?(a):(b))  
  38.   
  39. int num[500020];  
  40.   
  41. void getSumDivider(int n){  
  42.     int k,i,j;  
  43.     for(i = 0;i <= n;i++) num[i]=0;  
  44.     for(i = 2;i <= n;i++) num[i]+=1;  
  45.     for(i = 2;i+i <= n;i++){  
  46.         for(j = i+i,k=2;j <= n;j+=i,k++){  
  47.             num[j]+=k;  
  48.         }  
  49.     }  
  50. }  
  51. int main(){  
  52.     freopen("1008.out","w",stdout);  
  53.     int a,b,aaa,bbb,i,ans;  
  54.     getSumDivider(MAXN);  
  55.     //while(scanf("%d %d",&a,&b)!=EOF){  
  56.         a = 1;b = 500000;  
  57.         ans=0;  
  58.         for(i = a;i <= b;i++){  
  59.             aaa = num[i];  
  60.             if(aaa>b)continue;  
  61.             bbb = num[aaa];  
  62.             if(bbb == i && a <= aaa && aaa <= b && aaa != bbb)  
  63.             {  
  64.                 printf("%d,%d,\n",i,aaa);  
  65.                 ans++;  
  66.             }  
  67.         }  
  68.         printf("%d\n",ans/2);  
  69.     //}  
  70.     return 0;  
  71. }  

这是我最终AC 的代码:

[cpp] view plaincopy
  1. /* 
  2.  * @user ipqhjjybj 
  3.  * @Time 
  4.  * @data 20130629 
  5.  */  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. int Ans[56][2]={  
  9.     220,284,  
  10. 284,220,  
  11. 1184,1210,  
  12. 1210,1184,  
  13. 2620,2924,  
  14. 2924,2620,  
  15. 5020,5564,  
  16. 5564,5020,  
  17. 6232,6368,  
  18. 6368,6232,  
  19. 10744,10856,  
  20. 10856,10744,  
  21. 12285,14595,  
  22. 14595,12285,  
  23. 17296,18416,  
  24. 18416,17296,  
  25. 63020,76084,  
  26. 66928,66992,  
  27. 66992,66928,  
  28. 67095,71145,  
  29. 69615,87633,  
  30. 71145,67095,  
  31. 76084,63020,  
  32. 79750,88730,  
  33. 87633,69615,  
  34. 88730,79750,  
  35. 100485,124155,  
  36. 122265,139815,  
  37. 122368,123152,  
  38. 123152,122368,  
  39. 124155,100485,  
  40. 139815,122265,  
  41. 141664,153176,  
  42. 142310,168730,  
  43. 153176,141664,  
  44. 168730,142310,  
  45. 171856,176336,  
  46. 176272,180848,  
  47. 176336,171856,  
  48. 180848,176272,  
  49. 185368,203432,  
  50. 196724,202444,  
  51. 202444,196724,  
  52. 203432,185368,  
  53. 280540,365084,  
  54. 308620,389924,  
  55. 319550,430402,  
  56. 356408,399592,  
  57. 365084,280540,  
  58. 389924,308620,  
  59. 399592,356408,  
  60. 430402,319550,  
  61. 437456,455344,  
  62. 455344,437456,  
  63. 469028,486178,  
  64. 486178,469028  
  65. };  
  66. int main(){  
  67.     int a,b,ans,i;  
  68.     while(scanf("%d %d",&a,&b)!=EOF){  
  69.         ans=0;  
  70.         for(i=0;i<56;i++)  
  71.             if(a<=Ans[i][0]&&Ans[i][0]<Ans[i][1]&&Ans[i][1]<=b)  
  72.                 ans++;  
  73.         printf("%d\n",ans);  
  74.     }  
  75.     return 0;