Prime factor Decomposition: nefu118 (how many 0 are behind n!) + nefu119 (combined primes)

Source: Internet
Author: User
Tags ming stdin

how many 0 in the back of n!?

Description
Reads a number n from the input and evaluates n. The number of the end 0.
input
The input has several lines. The first line has an integer m, indicating the number of digits that follow. Then the M-line, each row contains a positive integer n,1<=n<=1000000000 that is determined.
Output
For each data in the input row, n, the output line, whose content is N. The number of the end 0.
Sample_input
3
3
1024
Sample_output
0
253
Problem Solving Ideas:

The minimum value of the factor 2 and 5 power is obtained by the decomposition of the element factor.

Number theory knowledge:the power of prime p in n! factor decomposition is [n/p]+[n/p^2]+[n/p^3]+[n/p^4]+ ...

Reference Code:

#include 
#include 
#include 
#include #include #include
using namespace std;
const int MAXN=50000+10;
int n;
int solve (int p)
{
   int cnt=0;
   int q=p;
   while (q<=n) {
    cnt+=n/q;
    q*=p;
   }
   return cnt;
}
int main ()
{
   //freopen ("Input.txt", "R", stdin);
    int t;cin>>t;
    while (t--) {
      cin>>n;
      int ans=min (Solve (2), solve (5));
      cout<

Combined primes

Description
Xiao Ming's father came back from the outside to bring her a gift, Xiao Ming happy to run back to his room, opened a look is a very large board (very large), Xiao Ming was disappointed. But not a few days found the big board of fun. The number of non-descending paths from the starting point (0,0) to the end point (N,n) is C (2n,n), and now xiaoming randomly takes out 1 prime numbers p, and he wants to know how many times C (2n,n) is exactly divisible by P. Xiao Ming thought for a long time have not thought out, now want to ask you to help Xiao Ming solve this problem, for you should not be difficult!
input
There are multiple sets of test data. The
first line is a positive integer t, which represents the number of groups of test data. The next 2 numbers for each group are the values of N and P, 1&lt;=n,p&lt;=1000000000 here.
Output
For each set of test data, the output line gives the number of times C (2n,n) is divisible by prime p, and when it is not divisible, the number is 0.
Sample_input
2
2 2)
2 3
Sample_output
1
1

Problem Solving Ideas:

Factor decomposition, based on the knowledge of number theory in nefu118, requires only (2n). Minus twice times the power of P (n). The power of P;

Pit in the q=q*p,q may be data is out, (p in 0-1000, 000,000, Range), big data will be error, change to a long long can.

Reference Code:

#include 
#include 
#include 
#include #include #include
using namespace std;
const int MAXN=50000+10;
int p;
int solve (int n)
{
   int cnt=0;
   Long long q=p;
   while (q<=n) {
    cnt+=n/q;
    q*=p;
   }
   return cnt;
}
int main ()
{
 //  freopen ("Input.txt", "R", stdin);
   int n;
    int t;cin>>t;
    while (t--) {
      cin>>n>>p;
      int Ans=solve (2*n) -2*solve (n);
      cout<





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.