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

55 篇文章 0 订阅
29 篇文章 0 订阅
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! = 1206! = 7207! = 50408! = 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|1
typedef 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|1
typedef 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)

*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值