Codeforces Round #105 (Div. 2) / 148A Insomnia cure (枚举 || 数论)

A. Insomnia cure
http://codeforces.com/problemset/problem/148/A
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

«One dragon. Two dragon. Three dragon», — the princess was counting. She had trouble falling asleep, and she got bored of counting lambs when she was nine.

However, just counting dragons was boring as well, so she entertained herself at best she could. Tonight she imagined that all dragons were here to steal her, and she was fighting them off. Every k-th dragon got punched in the face with a frying pan. Every l-th dragon got his tail shut into the balcony door. Every m-th dragon got his paws trampled with sharp heels. Finally, she threatened every n-th dragon to call her mom, and he withdrew in panic.

How many imaginary dragons suffered moral or physical damage tonight, if the princess counted a total of d dragons?

Input

Input data contains integer numbers k, l, m, n and d, each number in a separate line (1 ≤ k, l, m, n ≤ 101 ≤ d ≤ 105).

Output

Output the number of damaged dragons.

Sample test(s)
input
1
2
3
4
12
output
12
input
2
3
4
5
24
output
17
Note

In the first case every first dragon got punched with a frying pan. Some of the dragons suffered from other reasons as well, but the pan alone would be enough.

In the second case dragons 1, 7, 11, 13, 17, 19 and 23 escaped unharmed.


暴力代码:

/*30ms,0KB*/

#include <cstdio>

int main(void)
{
	int k, l, m, n, d, s = 0;
	scanf("%d%d%d%d%d", &k, &l, &m, &n, &d);
	for (int i = 1; i <= d; ++i)
		if (!(i % k && i % m && i % n && i % l))
			s++;
	printf("%d\n", s);
	return 0;
}


神牛写的数论代码:(没看懂)

/*30ms,0KB*/

#include <cstdio>
#include <vector>
using namespace std;

int v[4], n;
bool bio[4][4];

inline int gcd(int a, int b)
{
	int ret = 1;
	for (int i = 2; i <= a; ++i)
		if (a % i == 0 && b % i == 0)
			ret = i;
	return ret;
}

inline int lcm(int a, int b)
{
	return a / gcd(a, b) * b;///为防止可能的数据超范围,先除再乘要更好。
}

vector<int> rek(int i, int k, vector<int> w)
{
	if (k == 0)
	{
		int ret = w[0];
		for (int j = 1; j < (int) w.size(); ++j)
			ret = lcm(ret, w[j]);
		return vector<int> (1, ret);
	}
	if (i == 4)
        return vector<int>();
	vector<int> ret;
	vector <int> ret2 = rek(i + 1, k, w);
	for (int j = 0; j < (int)ret2.size(); ++j)
		ret.push_back(ret2[j]);
	w.push_back(v[i]);
	vector<int> ret1 = rek(i + 1, k - 1, w);
	for (int j = 0; j < (int)ret1.size(); ++j)
		ret.push_back(ret1[j]);
	return ret;
}

int main(void)
{
	int ret = 0;
	scanf("%d%d%d%d%d", &v[0], &v[1], &v[2], &v[3], &n);
	for (int i = 1; i <= 4; ++i)
	{
		vector<int> div = rek(0, i, vector<int>());
		for (int j = 0; j < (int)div.size(); ++j)
			if (i % 2)
				ret += n / div[j];
			else
				ret -= n / div[j];
	}
	printf("%d",ret);
}


你可能感兴趣的:(ACM,codeforces)