Description
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
Sample Input
3
9
5
2
Sample Output
Case 1: 2
Case 2: 1
Case 3: 1
Hint
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#define edge segtree[id]
#define lson segtree[id<<1]
#define rson segtree[id<<1|1]
using namespace std;
typedef long long ll;
const int N=1e6+5;
bool mark[N];
int prim[N];
int cnt;
//題目N取值 1<=N<=1e12
//T <=1e4
void initial()
{
cnt=0;
for (int i=2 ; i<N ; ++i)
{
if (!mark[i])
prim[cnt++]=i;
for (int j=0 ; j<cnt && i*prim[j]<N ; ++j)
{
mark[i*prim[j]]=1;
if (!(i%prim[j]))
break;
}
}
}
ll divi(ll n)
{
ll ans=1;
for (int i=0 ; i<cnt && prim[i]*prim[i]<=n ; ++i)
{
if (!(n%prim[i]))
{
int s=0;
while (!(n%prim[i]))
{
n/=prim[i];
s++;
}
ans*=(s+1);
}
}
return n>1?ans*(1+1):ans;//n>1代表n爲素數 也是一個正因子 所以要加上
}
int main()
{
initial();
int T;
scanf("%d",&T);
for (int test=1 ; test<=T ; test++)
{
ll n;
scanf("%lld",&n);
printf("Case %d: %lld\n",test,divi(n)-1);//多了一個1因子 刪去
}
return 0;
}
Description
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
Sample Input
2
10 4 1 1
100 5 40 5
Sample Output
Case 1: 1
Case 2: 6
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#define edge segtree[id]
#define lson segtree[id<<1]
#define rson segtree[id<<1|1]
using namespace std;
typedef long long ll;
const int N=1e6+5;
ll five[N],two[N];
void initial()
{
memset(five,0,sizeof(five));
memset(two,0,sizeof(two));
ll s1=0,s2=0;
for (int i=2 ; i<N ; ++i)
{
int num=i;
while (!(num%2))
{
s1++;
num/=2;
}
num=i;
while (!(num%5))
{
s2++;
num/=5;
}
two[i]=s1;
five[i]=s2;
}
}
int main()
{
initial();
int T;
scanf("%d",&T);
for (int test=1 ; test<=T ; ++test)
{
int n,r,p;
ll q;
scanf("%d%d%d%lld",&n,&r,&p,&q);
ll ans = min (two[n]-two[n-r]-two[r]+(two[p]-two[p-1])*q,five[n]-five[n-r]-five[r]+(five[p]-five[p-1])*q);
printf("Case %d: %lld\n",test,ans);
}
return 0;
}
Description
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
Sample Input
3
1
2
5
Sample Output
Case 1: 5
Case 2: 10
Case 3: impossible
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#define edge segtree[id]
#define lson segtree[id<<1]
#define rson segtree[id<<1|1]
using namespace std;
typedef long long ll;
const int N=1e8;//最多1e8個0
const int M=1e6;
int sum[M];
int cnt;
void initial()//因爲2的個數肯定系夠的,甘姐系記錄5的個數就得了
{
sum[0]=1;
int res=0;
cnt=0;
for (int i=1 ; ; ++i)
{
if (!(i%5))//看這個數有多少5
{
int s=0,m=i;
while (!(m%5))
{
s++;
m/=5;
}
res+=(s+1);
}
else
res+=1;
if (!(i%100))
{
sum[i/100]=res;
cnt++;
}
if (res>N)
break;
}
}
int main()
{
initial();
int T;
scanf("%d",&T);
for (int test=1 ; test<=T ; ++test)
{
int n;
scanf("%d",&n);
printf("Case %d: ",test);
if (n==1)
{
printf("5\n");
continue;
}
int ans=lower_bound(sum,sum+cnt,n)-sum-1;
ll res=sum[ans];
if (res==n)
{
printf("%lld\n",ans*500);
continue;
}
ll last;
if (!ans)
{
for (int i=2 ; ; ++i)
{
if (!(i%5))
{
int s=0,m=i;
while(!(m%5))
{
s++;
m/=5;
}
res+=(s+1);
}
else
res++;
if (res>=n)
{
if (res==n)
last=i;
else
last=-1;
break;
}
}
}
else
{
last=ans*100;
for (int i=last+1 ; ; ++i)
{
if (!(i%5))
{
int s=0,m=i;
while (!(m%5))
{
s++;
m/=5;
}
res+=(s+1);
}
else
res++;
if (res>=n)
{
if (res==n)
last=i;
else
last=-1;
break;
}
}
}
if (last==-1)
printf("impossible\n");
else
printf("%lld\n",last*5);
}
return 0;
}