n!素因子分解中素数p的幂

n!素因子分解中素数p的幂为 [n/p]+[n/(p^2)]+[n/(p^3)]+……

nefu 118 传送门

n!后面有多少个0

Problem : 118

Time Limit : 1000ms

Memory Limit : 65536K

description

从输入中读取一个数n,求出n!中末尾0的个数。

input

输入有若干行。第一行上有一个整数m,指明接下来的数字的个数。然后是m行,每一行包含一个确定的正整数n,1<=n<=1000000000。

output

对输入行中的每一个数据n,输出一行,其内容是n!中末尾0的个数。

sample_input

3
3
100
1024

sample_output

0
24
253
 

对于任意一个正整数 若对其进行因式分解 那么其末尾的0必可以分解为2*5 

在这里 每一个0 必然和一个因子5相对应 

但 一个数的因式分解中因子5不一定对应着一个0 因为还需要一个因子2 

而对于n! 在因式分解中  2的因子个数要大于5的因子个数  

所以这题只要求出5的幂就可以了

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)    //输出外挂
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

int main()
{
    //fread;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        int n;
        scanf("%d",&n);
        int five=5;
        int sum=0;
        while(five<=n)
        {
            sum+=n/five;
            five*=5;
        }
        printf("%d\n",sum);
    }
    return 0;
}

nefu 119  传送门

组合素数

Problem : 119

Time Limit : 1000ms

Memory Limit : 65536K

description

小明的爸爸从外面旅游回来给她带来了一个礼物,小明高兴地跑回自己的房间,拆开一看是一个很大棋盘(非常大),小明有所失望。不过没过几天发现了大棋盘的好玩之处。从起点(0,0)走到终点(n,n)的非降路径数是C(2n,n),现在小明随机取出1个素数p, 他想知道C(2n,n)恰好被p整除多少次?小明想了很长时间都没想出来,现在想请你帮助小明解决这个问题,对于你来说应该不难吧!

input

有多组测试数据。
第一行是一个正整数T,表示测试数据的组数。接下来每组2个数分别是n和p的值,这里1&lt;=n,p&lt;=1000000000。

output

对于每组测试数据,输出一行,给出C(2n,n)被素数p整除的次数,当整除不了的时候,次数为0。

sample_input

2
2 2
2 3

sample_output

1
1
 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)    //输出外挂
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

int main()
{
    //fread;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        int n,p;
        scanf("%d%d",&n,&p);
        int sum=0;
        double s=log(2.0*n)/log(1.0*p);
        int q=(int)s;
        int num=1;
        for(int i=1;i<=q;i++)
        {
            num*=p;
            sum=sum+(int)(2*n/num)-2*(int)(n/num);
        }
        printf("%d\n",sum);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值