12216-how many bases?
Time limit:3.000 seconds
A classical problem of number theory is "Find the number of trailing zeroes in NM, in base B". This problem are quite conventional and easy. But a number can has same number of trailing zeroes in more than one base. For example, if decimal number are written in 3, 4, 6, 8, and based number system, it'll look like 80, 60, 40, 3 0, and Ten respectively. So's all number systems it had only one trailing zero. Given A number NM, your job is to find out the number of an integer bases in which it has exactly T trailing zeroes.
Input
The input file contains at most 10000 line of input. Each line contains three integers N (1≤n≤108), M (0< m≤107) and T (0< t≤104). The meaning of N, M and T are given in the problem statement. Input is terminated by a line containing three zeroes, which obviously should not being processed for calculation.
Output
For each line of input produce one line of output. This line contains the serial for output followed by a integer NB, which is modulo 100000007 value of number of bases in W Hich NM has exactly T trailing zeroes.
Sample input Output for sample input
| 24 1 1 100 200 10 23 18 2 0 0 0 |
Case 1:6 Case 2:312 Case 3:3 |
Test instructions: Input n,m,k, you will be the M of N to use some kind of binary representation, its tail is exactly k
0. Ask for the number of such a binary.
Idea: Suppose that the M-order of N is represented by the P-binary, and its tail is exactly K 0. is actually equivalent to
N^m% p^k = 0 && n^m% p^ (k+1)! = 0.
Ans1 means that the M-time of N is represented by a certain binary, and its tail has at least the number of digits of K 0.
Ans2 means that the M-time of N is represented by a certain binary, and its tail has at least k+1 0 of the number of binary.
If FA is a mass factor of N, there is TP * (fa^num) = n; The number of FA in N^m is cnt=num*m.
fa^cnt = fa ^ (cnt/k) ^ K. namely ans1=ans1* (cnt/k+1);
fa^cnt = fa ^ (CNT/(k+1)) ^ (k+1). i.e. ans2=ans2* (CNT/(k+1) +1);
Final answer ans = ans1-ans2;
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;
const int mod = 100000007;
ll ans1,ans2;
int n,m,k,cnt;
int main()
{
while(scanf("%d %d %d",&n,&m,&k)!=EOF)
{
if(n==0 && m==0 && k==0) break;
ans1=ans2=1;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
int cnt=0;
while(n%i==0)
{
n/=i;
cnt++;
}
cnt*=m;
ans1=ans1*(cnt/k+1)%mod;
ans2=ans2*(cnt/(k+1)+1)%mod;
}
}
if(n!=1)
{
ans1=ans1*(m/k+1)%mod;
ans2=ans2*(m/(k+1)+1)%mod;
}
printf("Case %d: %lld\n",++cnt,(ans1-ans2+mod)%mod);
}
return 0;
}
Uva 12216 How many bases? (Maths problem)