Factorial factorization of ac number theory

Factorial factorization (2)

Time Limit: 3000  ms | Memory Limit: 65535  KB
Difficulty: 3
describe

Given two numbers n, m, where m is a prime number.

Decompose the factorial of n (0<=n<=2^31) into prime factors, and find how many m there are.

Note: ^ is the symbol for exponentiation.

 

enter
The first line is an integer s (0<s<=100), indicating the number of groups of test data. The
subsequent  s lines, each line has two integers n, m.
output
output the number of m
sample input
3
100 5
16 2
1000000000  13
Sample output
24
15

83333329

If this problem analyzes each multiplication term side by side, it will definitely time out, so what should I do?

Assuming that there are m, 2m.... there are a total of n/m numbers that are divisible by m, then this number of m must have at least one m, then there are n/m m

Then there must be n/(m*m) numbers with an m factor that can be divisible by m^2 of these n/m digital clocks, then there are n/m+n/(m*m) m numbers.

...

Only until n/m=0 means that there is no extra m left for the remaining numbers

#include<iostream>
using namespace std;
intmain()
{
    int ncase;
    cin>>ncase;
    while(ncase--)
    {
        int n,m;
        cin>>n>>m;
        int cnt=0;
        //The first time is m, 2m, 3m.... There is an m in it, and there are a total of n/m of these numbers
        //The remaining n/m numbers that are divisible by m are n/m/m
        //leftover. . . . .
        while(n/m!=0)
        {
            cnt+=n/m;
            n/=m;
        }
        cout<<cnt<<endl;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483826&siteId=291194637