T-Prime(数学规律)
来源:互联网 发布:php入门经典 pdf 编辑:程序博客网 时间:2024/06/05 03:32
Description
We know that prime numbers are positive integers that have exactly two distinct positive divisors.
Similarly, we’ll call a positive integer t Т-prime, if t has exactly three distinct positive divisors。
You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.
Input
The first line contains a single positive integer, n (1 ≤ n ≤ 10000),showing how many numbers are in the array.
The next line contains n space-separated integers xi (1 ≤ xi ≤ 10^12).
Output
Print one line: The number of T-primes number
Sample Input
Copy sample input to clipboard
3
4 5 6
Sample Output
1
Hint
Please use long long instead of int for any Xi.
The given test has three numbers.
The first number 4 has exactly three divisors — 1, 2 and 4.
The second number 5 has two divisors (1 and 5),
The third number 6 has four divisors (1, 2, 3, 6),
hence the answer for them is 1 (only the number 4 is T-primes number).
代码实现
#include<iostream>#include<cmath>using namespace std;long long data[10000];bool isTPrime(long long n){ long long temp = sqrt(n); if(temp * temp == n) { if(temp < 2) return false; else { for(long long i = 2; i <= sqrt(temp); i++){ if(temp % i == 0){ return false; } } return true; } } else return false;}int main(){ int n; while(cin >> n){ int result = 0; for(long long i = 0; i < n; i++){ cin >> data[i]; } for(long long i = 0; i < n; i++){ if(isTPrime(data[i])){ result ++; } } cout << result << endl; } return 0;}
一个数一定可以被 1 和 它本身整除。所以要考虑的是是否存在另外一个数可以整除n。
假如存在这么一个数a,
那么显然(n/a)也可以整除n。
那么也就是说 a = (n / a);
a * a = n;
所以假如 n 是一个T-Prime, 那么 n 必然是完全平方数,并且其平方根必须是素数。
由此,我们可以得到一个判定T-Prime的方法。
- T-Prime(数学规律)
- HDU--5312(规律+数学)
- hdu-1220(数学规律)
- FZU 2147(数学,规律)
- 摆方格 (数学规律)
- CSU 1756 Prime(数学+判重)
- uva 846 Steps(数学规律)
- uva 10706 Number Sequence(数学规律)
- HDU 4279(数学+找规律)
- nyoj1087摆方格(数学规律题)
- Friend 1719 (数学+技巧+规律)
- nyoj 摆方格 1087 (数学规律)
- poj Ants 1852 (简单数学规律)
- zzulioj--1609--求和(数学规律)
- hdoj geometry 5605 (简单数学规律)
- CodeForces - 630D Hexagons! (数学规律)
- CodeForces675BRestoring Painting(数学规律计算)
- hdoj2073无限的路(数学规律)
- 初识Spring MVC框架
- C语言 实现中缀表达式转后缀表达式并求值
- 利用SharedPreferences对象进行读写
- java equals和hashcode、==、的关系,使用区别详细
- .net 自定义attribute使用
- T-Prime(数学规律)
- Unity3d的Mono编辑不了的问题
- TCP和UDP的区别(转)
- Spring的ORM模块(结合Hibernate)
- 视频大小与帧的计算
- 187. Repeated DNA Sequences
- Android之Hierarchy Viewer工具使用
- Anroid 安全卫士第一天_注意事项
- Spring的AOP模块