Trailing Zeroes (I)
We know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. But in different bases we use different symbols. For example in binary number system we use only 0 and 1. Now in this problem, you are given an integer. You can convert it to any base you want to. But the condition is that if you convert it to any base then the number in that base should have at least one trailing zero that means a zero at the end.
For example, in decimal number system 2 doesn’t have any trailing zero. But if we convert it to binary then 2 becomes (10)2 and it contains a trailing zero. Now you are given this task. You have to find the number of bases where the given number contains at least one trailing zero. You can use any base from two to infinite.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer N (1 ≤ N ≤ 1012).
Output
For each case, print the case number and the number of possible bases where N contains at least one trailing zero.
Sample Input
3
9
5
2
Sample Output
Case 1: 2
Case 2: 1
Case 3: 1
Note
For 9, the possible bases are: 3 and 9. Since in base 3; 9 is represented as 100, and in base 9; 9 is represented as 10. In both bases, 9 contains a trailing zero..
分析: 模拟几个数字,很明显 就是N有几个(非1)因子 。
时间复杂度 o(T*N^(1/4) )
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
const int N = 1e6+11;
const int M = 1e6+11;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
bool su[N+1]={1,1,0}; int prm[N+1],sz=0;
void init(){
for(int i=2;i<N;i++){
if(!su[i]) prm[sz++]=i;
for(int j=0;j<sz;j++){
int t=prm[j]*i;
if(t>N) break;
su[t]=1;
if(i%prm[j]==0) break;
}
}
}
LL solve(LL n){
LL ans=1;
for(int i=0;i<sz && prm[i]*1ll*prm[i]<=n;i++){
if(n%prm[i]==0){
LL cnt=0;
while(n%prm[i]==0) {
cnt++; n/=prm[i];
}
ans=ans*(cnt+1);
}
}
if(n!=1) ans<<=1;
return ans-1;
}
int main(){
init();
int T ;scanf("%d",&T);
int cas=1;
while(T--){
LL n;scanf("%lld",&n);
printf("Case %d: %lld\n",cas++,solve(n));
}
return 0;
}
Trailing Zeroes (II)
Find the number of trailing zeroes for the following function:
nCr * pq
where n, r, p, q are given. For example, if n = 10, r = 4, p = 1, q = 1, then the number is 210 so, number of trailing 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 of trailing zeroes.
Sample Input
2
10 4 1 1
100 5 40 5
Sample Output
Case 1: 1
Case 2: 6
分析: C(n,r) = n! / ( m ! * (n-r) ! ) 所以先求这里面的2和5的质因子个数。
然后再求 p^q 中的 质因子2和5的个数, 可以先求p的个数,然后都乘以q就行了。
最后 取最小个数就行了。’
代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
const int N = 1e6+11;
const int M = 1e6+11;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
int main(){
int T ;scanf("%d",&T);
int cas=1;
while(T--){
LL n,r,p,q; scanf("%lld%lld%lld%lld",&n,&r,&p,&q);
LL two,five;
two=five=0;
LL m=n;
while(m){
two+=m/2;
m/=2;
}
m=n;
while(m){
five+=m/5;
m/=5;
}
m=r;
while(m){
two-=m/2;
m/=2;
}
m=r;
while(m){
five-=m/5;
m/=5;
}
m=n-r;
while(m){
two-=m/2;
m/=2;
}
m=n-r;
while(m){
five-=m/5;
m/=5;
}
LL a,b;a=b=0;
m=p;
while(m%2==0){
a++;
m/=2;
}
while(m%5==0){
b++;
m/=5;
}
a*=q; b*=q;
LL ans=min(a+two,b+five);
printf("Case %d: %lld\n",cas++,ans);
}
return 0;
}
Trailing Zeroes (III)
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*…*N. For example, 5! = 120, 120 contains one zero on the trail.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
Output
For each case, print the case number and N. If no solution is found then print ‘impossible’.
Sample Input
3
1
2
5
Sample Output
Case 1: 5
Case 2: 10
Case 3: impossible
分析:二分 即可 。
代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
const int N = 1e6+11;
const int M = 1e6+11;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int LL inff = 0x3f3f3f3f3f3f3f3f;
LL Solve(LL n){ // 求n!的末尾0的个数
LL a,b; a=0;b=0;
LL m=n;
while(m){
a+=m/2;
m/=2;
}
m=n;
while(m) {
b+=m/5;
m/=5;
}
return min(a,b);
}
int main(){
int T ;scanf("%d",&T);
int cas=1;
while(T--){
int q;scanf("%d",&q);
LL l=0,r=inff; LL ans=-1;
while(l<=r){
LL mid=(l+r)>>1;
LL tmp=Solve(mid);
if(tmp>=q){
if(tmp==q) ans=mid;
r=mid-1;
}else l=mid+1;
}
if(ans==-1) printf("Case %d: impossible\n",cas++,ans);
else printf("Case %d: %lld\n",cas++,ans);
}
return 0;
}