数论 - 筛法暴力打表 --- hdu : 12876 Quite Good Numbers
#
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. |
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: |
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 |
Mean:
让你求从sta到end这个区间中有多少个数满足:abs(sum-i)<=bad。其中sum是i所有的因子之和,bad是给定的值,代表误差。
analyse:
由于数字很大,必须打表,我们将10^6次方内i的因子之和求出来。
从筛法求素数得到的启发,方法很巧妙,具体看代码。
Time complexity:O(n)
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | //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; } |
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3901754.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?