数论 - 筛法暴力打表 --- hdu : 12876 Quite Good Numbers
2014-08-09 23:13
483 查看
Quite Good Numbers |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
Total submit users: 77, Accepted users: 57 |
Problem 12876 : No special judgement |
Problem description |
A "perfect" number is an integer that is equal to the sum of its divisors (where 1 is considered a divisor). For example, 6 is perfect because its divisors are 1, 2, and 3, and 1 + 2 + 3 is 6. Similarly, 28 is perfect because it equals 1 + 2 + 4 + 7 + 14. A "quite good" number is an integer whose "badness" ? the absolute value of the difference between the sum of its divisors and the number itself ? is not greater than a specified value. For example, if the allowable badness is set at 2, there are 11 "quite good" numbers less than 100: 2, 3, 4, 6, 8, 10, 16, 20, 28, 32, and 64. But if the allowable badness is set at 0 (corresponding to the "perfect" numbers) there are only 2: 6 and 28. Your task is to write a program to count how many quite good numbers (of a specified maximum badness) fall in a specified range. |
Input |
Input will consist of specifications for a series of tests. Information for each test is a single line containing 3 integers with a single space between items: • start (2 <= start < 1000000) specifies the first number to test • stop (start <= stop < 1000000) specifies the last number to test • badness (0 <= badness < 1000) specifies the maximum allowable badness A line containing 3 zeros terminates the input. |
Output |
Output should consist of one line for each test comprising the test number (formatted as shown) followed by a single space and the number of values in the test range with badness not greater than the allowable value. |
Sample Input |
2 100 2 2 100 0 1000 9999 3 0 0 0 |
Sample Output |
Test 1: 11 Test 2: 2 Test 3: 6 |
Problem Source |
HNU Contest |
让你求从sta到end这个区间中有多少个数满足:abs(sum-i)<=bad。其中sum是i所有的因子之和,bad是给定的值,代表误差。
[b]analyse:[/b]
由于数字很大,必须打表,我们将10^6次方内i的因子之和求出来。
从筛法求素数得到的启发,方法很巧妙,具体看代码。
[b]Time complexity:[/b]O(n)
[b]Source code:[/b]
//Memory Time // 4988K 40MS // by : Snarl_jsb #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack> #include<iomanip> #include<string> #include<climits> #include<cmath> #define MAX 1000005 #define LL long long using namespace std; int sta,stop,bad,ans,kase=1; int sum[MAX]; void make_table() { for(int i=2;i<=MAX;i++) { sum[i]++; for(int j=2;i*j<=MAX;j++) sum[i*j]+=i; } } int main() { make_table(); while(scanf("%d %d %d",&sta,&stop,&bad),ans=0,sta+stop+bad) { printf("Test %d: ",kase++); for(int i=sta;i<=stop;i++) { if(abs(sum[i]-i)<=bad) ans++; } cout<<ans<<endl; } return 0; }
相关文章推荐
- 数论 - 筛法暴力打表 --- hdu : 12876 Quite Good Numbers
- HNU 12876 Quite Good Numbers 完美数变形
- HDU 1058 Humble Numbers(打表+暴力)
- HDU-1058-Humble Numbers(暴力打表)
- HOJ 13083 Quite Good Numbers(其实是暴力)
- hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)
- hdu 2521 筛法暴力之
- hdu Humble Numbers(打表)
- HDU 1215(数论,打表 - -|)
- hdu 5676 ztr loves lucky numbers 打表+二分
- HDU 5812 Distance 暴力+素数打表
- hdu 1215 数论 +打表
- hdu Humble Numbers(打表)
- HDU 4320 Arcane Numbers 1 (数论)
- hdu 5750 Dertouzos(数论,打表,暴力)
- POJ3292 UVA11105 Semi-prime H-numbers【筛法打表】
- hdu 2136 数论+筛选质数+打表
- hdu 2854 Central Meridian Number--数论--暴力--推导
- hdu Humble Numbers(打表)
- HDU 4320 Arcane Numbers 1 (数论)