Trailing Zeroes (III)—LightOJ - 1138(二分).

题目链接

题目大意:求一个数N,N!的末尾连续0的个数为Q,求最小的N; 

思路:判断有多少个0,可以判断分解之后有多少个2和5。如果是阶乘的话,遇到的5的数量一定比2少,所以判断遇到的5的数量即可。
判断N!分解之后有多少个5,可以通过除5,25,125,625,来判断。每次除法只需要+N/5,+N/25,25出现的次数已经在5中算过一次。
所以直接加就可以。
比如 50!就是12个0    50/5+50/25=12。剩下的就是二分那个数后面有Q个0。

代码:
#include<iostream>
#include<algorithm>
using namespace std;
int z[500]={0,5};
int num(int n){    //求这个数末尾有多少个0.
	int ans=0;
	for(int i=1;z[i]<=n;i++){
		ans+=(n/z[i]);
	}
	return ans;
}
void bi(int q){   //二分求这个数
	int left=1,right=5e8;
	while(left<right){
		int mid=(left+right)/2;
		int cnt=num(mid);
		if(cnt==q){
			cout<<mid/5*5<<endl;
			return ;
		}
		if(cnt<q){
			left=mid+1;
		}
		else right=mid-1;
	}
	cout<<"impossible\n";
}

int main()
{
	for(int i=2;i<15;i++){
		z[i]=z[i-1]*5;
		//cout<<z[i]<<endl;
	}
	int t;
	cin>>t;
	int q;
	for(int i=1;i<=t;i++){
		cin>>q;
		cout<<"Case "<<i<<": ";
		bi(q);
	}
	return 0;
 } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
xiazai.py:10:0: C0301: Line too long (130/100) (line-too-long) xiazai.py:29:21: C0303: Trailing whitespace (trailing-whitespace) xiazai.py:30:0: W0311: Bad indentation. Found 10 spaces, expected 12 (bad-indentation) xiazai.py:40:0: C0301: Line too long (103/100) (line-too-long) xiazai.py:41:0: C0301: Line too long (153/100) (line-too-long) xiazai.py:53:0: C0305: Trailing newlines (trailing-newlines) xiazai.py:1:0: C0114: Missing module docstring (missing-module-docstring) xiazai.py:7:0: C0103: Constant name "url" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:13:13: W3101: Missing timeout argument for method 'requests.get' can cause your program to hang indefinitely (missing-timeout) xiazai.py:14:16: I1101: Module 'lxml.etree' has no 'HTML' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. (c-extension-no-member) xiazai.py:19:0: C0103: Constant name "num" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:21:4: R1723: Unnecessary "elif" after "break", remove the leading "el" from "elif" (no-else-break) xiazai.py:24:17: W3101: Missing timeout argument for method 'requests.get' can cause your program to hang indefinitely (missing-timeout) xiazai.py:25:20: I1101: Module 'lxml.etree' has no 'HTML' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. (c-extension-no-member) xiazai.py:28:8: C0103: Constant name "judge" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:28:31: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) xiazai.py:30:22: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) xiazai.py:31:14: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) xiazai.py:34:8: C0103: Constant name "chapter_num" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:38:29: W3101: Missing timeout argument for method 'requests.get' can cause your program to hang indefinitely (missing-timeout) xiazai.py:39:32: I1101: Module 'lxml.etree' has no 'HTML' member, but source is unavailable. Consider adding this module to extension-pkg-allow-list if you want to perform analysis based on run-time introspection of living objects. (c-extension-no-member) xiazai.py:41:22: C0209: Formatting a regular string which could be a f-string (consider-using-f-string) xiazai.py:42:16: C0103: Constant name "all_content" doesn't conform to UPPER_CASE naming style (invalid-name) xiazai.py:44:20: R1713: Consider using str.join(sequence) for concatenating strings from an iterable (consider-using-join) ----------------------------------- Your code has been rated at 5.43/10
07-15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值