LightOJ 1090 - Trailing Zeroes (II) (求式子结果0的个数)
来源:互联网 发布:js 生成压缩包 编辑:程序博客网 时间:2024/06/18 02:38
1090 - Trailing Zeroes (II)
![](http://www.lightoj.com/images/submit1.png)
![](http://www.lightoj.com/images/udebug.png)
Time Limit: 2 second(s)Memory Limit: 32 MB
Find the number of trailing zeroes for the followingfunction:
nCr * pq
where n, r, p, q are given. For example, if n =10, r = 4, p = 1, q = 1, then the number is210 so, number oftrailing zeroes is 1.
Input
Input starts with an integer T (≤ 10000),denoting the number of test cases.
Each case contains four integers: n, r, p, q (1 ≤n, r, p, q ≤ 106, r ≤ n).
Output
For each test case, print the case number and the number oftrailing zeroes.
Sample Input
Output for Sample Input
2
10 4 1 1
100 5 40 5
Case 1: 1
Case 2: 6
思路:首先,求出C(n,r)中2的个数和5的个数,怎么求呢,设F2(x)表示x!中2的个数,那么C(n,r)中2的个数就是
[F2(n)-F2[n-r]]/F2(r)
同理可以求出5的个数,然后就是求p^q中2和5的个数了,直接分解p然后结果乘q就行了,最后取cnt2和cnt5的最小值
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 101000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-8using namespace std;ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}//headint main(){ int n,r,p,q; int t,cas=0; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&n,&r,&p,&q); int nn=n; int cnt1=0,cnt2=0; int k=r; while(nn) cnt1+=nn/5,nn/=5; nn=n-r; while(nn) cnt1-=nn/5,nn/=5; nn=n; while(nn) cnt2+=nn/2,nn/=2; nn=n-r; while(nn) cnt2-=nn/2,nn/=2; while(k) cnt1-=k/5,k/=5; k=r; while(k) cnt2-=k/2,k/=2; while(p%2==0) p/=2,cnt2+=q; while(p%5==0) p/=5,cnt1+=q; printf("Case %d: %d\n",++cas,min(cnt1,cnt2)); }return 0;}
0 0
- LightOJ 1090 - Trailing Zeroes (II) (求式子结果0的个数)
- lightoj 1090 - Trailing Zeroes (II)
- LightOJ 1090 Trailing Zeroes (II)
- LightOJ 1028 1028 - Trailing Zeroes (I) (求因子个数)
- LightOJ 1138 Trailing Zeroes (III) (阶乘末尾0的个数)
- LightOJ 1090Trailing Zeroes (II) [分解]【数论】
- LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)
- LightOJ 1138 - Trailing Zeroes (III) (求末尾0为x的最小N---二分)
- LeetCode:求阶乘后面的0的个数(Factorial Trailing Zeroes)
- LeetCode 172. Factorial Trailing Zeroes(阶乘结果尾数0的个数)---E
- Factorial Trailing Zeroes 阶乘结果中后缀为0的个数
- leetcode解题之172# Factorial Trailing Zeroes Java版 (求n的阶乘末尾0的个数)
- Lightoj 1138 ( Trailing Zeroes (III))
- LightOJ 1138Trailing Zeroes (III)(二分)
- 【lightoj 1138】Trailing Zeroes (III) (二分)
- lightoj Trailing Zeroes (III)
- LightOJ 1138 Trailing Zeroes
- Trailing Zeroes (III) LightOJ
- Struts2框架提供的结果类型
- 数据结构一顺序表
- java设置classpath,path和java_home的作用
- 通达OA在centos系统中快速部署文档(web和数据库)
- C++调错技巧
- LightOJ 1090 - Trailing Zeroes (II) (求式子结果0的个数)
- android插上耳机仍用扬声器播放音频
- 推荐7个 CSS3 制作的创意下拉菜单效果
- 设置快捷键
- iOS开发之KVO机制
- UVA 11478 Halum 最小差分系统
- 二叉树的递归和非递归遍历方法
- RossWeatherMap天气地图项目中踩的雷(一):Viewpager动态删除Fragment
- android 开发文档模板