TOJ 3349 Counting Divisor / 素数筛选法
2013-10-23 20:15
211 查看
Counting Divisor
时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte描述
If an integer A can divide integer B exactly, then A can be called a divisor of B.
Now there are N (1 <= N <= 100,000) integesr, for each of these number, please count the number of divisor from the rest N-1 integers.
输入
The first line of input data is an integer N, the following N rows have a positive integer,and each integer isn't above 1,000,000。
输出
According to the input order output every integer in the rest N - 1 integer number of divisor.
样例输入
5 2 1 2 3 4
样例输出
2 0 2 1 3
这题真的做了很久 求助了很多人才过的 感谢学长 学友
主要是利用了素数筛选那样的方法
自己TLE无数
看代码 一看就恍然大悟
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; int cnt[1000010]; int map[1000010]; int a[100010]; int main() { int n,i,j,max = 0; scanf("%d",&n); for(i = 1;i <= n; i++) { scanf("%d",&a[i]); cnt[a[i]]++; if(max < a[i]) max = a[i]; } for(i = 1;i <= max; i++) { if(cnt[i]) { for(j = i + i; j <= max; j += i)//和素数筛选差不多 赞一个 { map[j] += cnt[i]; } } } for(i = 1;i <= n; i++) { if(cnt[a[i]] > 1) printf("%d\n",map[a[i]] + cnt[a[i]] - 1); else printf("%d\n",map[a[i]]); } return 0; }
相关文章推荐
- 不回调ServiceConnection中onServiceConnected()方法的问题
- Android优秀开源项目
- OCP-1Z0-052-V8.02-114题
- java 连接 MySQL
- linux内核树的建立 (转)
- opengl 教程(12) 投影矩阵
- IrrLicht入门基础
- 第一个mapreduce程序的测试与分析
- 关于Socket通讯时通讯协议的制定
- Use ResourceBundle read properties file.
- vs2010编译配置Boost 1.54.0
- 怎么都在投机取巧
- hdu——1032——The 3n + 1 problem
- 自创引擎II Demo
- Android 本地推送消息到通知栏 NotificationManager 、注册Android Service
- sip鉴权认证Authorization MD5
- OCP-1Z0-052-V8.02-113题
- 个人关于数据结构的看法
- LeetCode:Word Break(DP)
- mhdd扫描磁盘坏道快速使用方法