您的位置:首页 > Web前端

【Manthan, Codefest 16B】【二分 or 递推暴力】A Trivial Problem 输出阶乘为m的所有数

2016-02-28 13:32 357 查看
B. A Trivial Problem

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Mr. Santa asks all the great programmers of the world to solve a trivial problem. He gives them an integer m and
asks for the number of positive integers n, such that the factorial of n ends
with exactly m zeroes. Are you among those great programmers who can solve this
problem?

Input
The only line of input contains an integer m (1 ≤ m ≤ 100 000) —
the required number of trailing zeroes in factorial.

Output
First print k —
the number of values of n such that the factorial of n ends
with m zeroes. Then print these k integers
in increasing order.

Examples

input
1


output
5
5 6 7 8 9


input
5


output
0


Note
The factorial of n is
equal to the product of all integers from 1 to n inclusive,
that is n! = 1·2·3·...·n.
In the first sample, 5! = 120, 6! = 720, 7! = 5040, 8! = 40320 and 9! = 362880.

【二分解法】

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1#define rs o<<1|1typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
int main()
{
while (~scanf("%d", &n))
{
int l = 0;
int r = 1e9;
while (l < r)
{
int m = (l + r + 1) >> 1;
int five = 0;
int tmp = m;
while (tmp) { tmp /= 5; five += tmp; }
if (five > n)r = m - 1;
else l = m;
}
int R = l;
l = 0;
r = 1e9;
while (l < r)
{
int m = (l + r) >> 1;
int five = 0;
int tmp = m;
while (tmp) { tmp /= 5; five += tmp; }
if (five < n)l = m + 1;
else r = m;
}
int L = l;
printf("%d\n", R - L + 1);
for (int i = L; i <= R; ++i)printf("%d ", i);
puts("");
}
return 0;
}
/*
【题意】
给你一个数组n(1<=n<=1e5)
让你输出有多少数的阶乘后恰好有n个0,并依次输出。

【类型】
二分or暴力

【分析】
肯定满足,数字越大,其后的0的个数也就越多。
于是我们可以二分出最小的l,使得fac[l]>=n
同时我们二分出最大的r,使得fac[r]<=n
然后答案就是区间段[l,r]

而算fac[l]有多少个0,就是查看fac[l]中有多少个5
因为n不大,所以另外一种做法是暴力。
我们直接求出fac[i]的末尾有多少个0即可

【时间复杂度&&优化】
O(log(n)log(n)) or O(nlogn)

*/


【暴力递推法】

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1#define rs o<<1|1typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
void solve()
{
int l = -1;
int r = -2;
int zero = 0;
for (int i = 0; ; ++i)
{
for (int x = i; x && x % 5 == 0; x /= 5)++zero;
if (zero == n)
{
if (l == -1)l = i;
r = i;
}
else if (zero > n)break;
}
printf("%d\n", r - l + 1);
for (int i = l; i <= r; ++i)printf("%d ", i);
puts("");
}
int main()
{
while (~scanf("%d", &n))
{
solve();
}
return 0;
}
/*
【题意】
给你一个数组n(1<=n<=1e5)
让你输出有多少数的阶乘后恰好有n个0,并依次输出。

【类型】
二分or暴力

【分析】
肯定满足,数字越大,其后的0的个数也就越多。
于是我们可以二分出最小的l,使得fac[l]>=n
同时我们二分出最大的r,使得fac[r]<=n
然后答案就是区间段[l,r]

而算fac[l]有多少个0,就是查看fac[l]中有多少个5
因为n不大,所以另外一种做法是暴力。
我们直接求出fac[i]的末尾有多少个0即可

【时间复杂度&&优化】
O(log(n)log(n)) or O(nlogn)

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息