A - Harmonic Number LightOJ - 1234
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:
In this problem, you are given n, you have to find Hn.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 108).
Output
For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.
Sample Input
12
1
2
3
4
5
6
7
8
9
90000000
99999999
100000000
Sample Output
Case 1: 1
Case 2: 1.5
Case 3: 1.8333333333
Case 4: 2.0833333333
Case 5: 2.2833333333
Case 6: 2.450
Case 7: 2.5928571429
Case 8: 2.7178571429
Case 9: 2.8289682540
Case 10: 18.8925358988
Case 11: 18.9978964039
Case 12: 18.9978964139
这个题感觉不是很难,以前没碰到过就搜了一下看到了别人的思路
主要就是用到了欧拉函数, 需要注意的是,欧拉函数在很大时才会成立, 故小于10000时还需判断。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const double c = 0.57721566490153286060651209;
double ans[10000];
void ff(){
memset(ans, 0, sizeof(ans));
for(int i = 1; i < 10000; i++){
ans[i] = ans[i-1] + 1.0/i;
}
}
int main(){
int t;
cin>> t;
int n;
ff();
for(int i = 1; i <= t; i++){
cin>> n;
if(n < 10000)
printf("Case %d: %.10f\n", i, ans[n]);
else
printf("Case %d: %.10f\n", i, log(n)+c+1.0/(2*n));
}
return 0;
}
A - Factorial HDU - 1124 The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.
ACM technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called “Travelling Salesman Problem” and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4…N. The number is very high even for a relatively small N.
The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour of the factorial function.
For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1<N2, then Z(N1) <= Z(N2). It is because we can never “lose” any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.
Input
There is a single positive integer T on the first line of input. It stands for the number of numbers to follow. Then there is T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.
Output
For every number N, output a single line containing the single non-negative integer Z(N).
Sample Input
6
3
60
100
1024
23456
8735373
Sample Output
0
14
24
253
5861
2183837
AC代码:
#include<bits/stdc++.h>
using namespace std;//求N!的末尾有几个0
// 10 = 2 * 5
//2的个数大于5
int main(){
int t, cnt, n;
cin>> t;
while(t--){
cnt = 0;
cin>> n;
for(int i = 5; i <= n; i*=5){
cnt += n/i;
}
cout<< cnt <<endl;
}
return 0;
}
B - Goldbach`s Conjecture LightOJ - 1259
Goldbach’s conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:
Every even integer, greater than 2, can be expressed as the sum of two primes [1].
Now your task is to check whether this conjecture holds for integers up to 107.
Input
Input starts with an integer T (≤ 300), denoting the number of test cases.
Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).
Output
For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where
-
Both a and b are prime
-
a + b = n
-
a ≤ b
Sample Input
2
6
4
Sample Output
Case 1: 1
Case 2: 1
Note
-
An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...
思路:
数组必须写成bool,因为int占四个字节。
还有一点就是, 1e7中的素数肯定少于1e7/2,所以这样就不会TLE了。
AC代码:
#include<bits/stdc++.h>
using namespace std;//素数筛
#define N 10000005
bool a[N];
int prime[666666];
int main(){
int t, n, h, k = 1, num = 0, l = 0;
for(int i = 2; i <= 10000000;i++){
if(a[i] == false){
prime[l++] = i;
for(int j = i*2; j <= 10000000; j += i){
a[j] = true;
}
}
}
a[0] = a[1] = true;
cin>> t;
while(t--){
num = 0;
cin>> n;
for(int i = 0; i < l; i++){
if(prime[i] >= n/2+1){
break;
}
h = n - prime[i];
if(a[h] == false){
num++;
}
}
printf("Case %d: %d\n", k, num);
k++;
}
}