LightOJ 1090 - Trailing Zeroes (II) (求式子结果0的个数)
2016-03-30 21:45
726 查看
1090 - Trailing Zeroes (II)
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.
Each case contains four integers: n, r, p, q (1 ≤n, r, p, q ≤ 106, r ≤ n).
思路:首先,求出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代码:
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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-8 using 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;} //head int 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; }
相关文章推荐
- Xenomai 的模式切换浅析
- Light oj 1236 - Pairs Forming LCM (约数的状压思想)
- has leaked ServiceConnection com.baidu.location.LocationClient
- vmware 安装 vmware tools 报Mounting HGFS filesystems: [FAILED]
- 【Data Algorithms_Recipes for Scaling up with Hadoop and Spark】Chapter 11 Smarter Email Marketing wit
- Waiting For Debugger
- 107页Pacific Trails Resort案例分析
- Thread详解9:用wait/notify实现生产者/消费者模式
- nginx报错accept() failed (24: Too many open files) 分析解决
- runtime运行时 http://blog.csdn.net/ComeOnZhao/article/details/50982808
- weblogic一个domain配置多个端口
- sendemail + cygwin
- LeetCode 217 Contains Duplicate
- lvchange的available參数
- 小戴人工智能PurposeAI-20180201
- 解决Sublime包管理package control 报错 There are no packages available for installation
- 问题人生[20160330] -[NSCFString containsString:]: unrecognized selector sent to instance 0x7f9902724da0
- UvaLive 6667 Longest Chain (分治求三元组LIS&树状数组)
- UVALive 2965-Jurassic Remains (Mitm)中途相遇法+bitmask
- Canvas(画布)、Paint(画笔) 详解