poj 1150 数论

排列数的最后一个非零数字

一个做了好久的题目
    2*5会产生0,其余的因子都不能
    所以把所有的因子2 5提取出来,然后统计剩下的37 9的个数,自己计算循环节及可得出答案。
     


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=10+9;
int c[maxn];
int cal(int n,int tmp)
{
    int ans=0;
    while(n/tmp)
    {
        ans+=n/tmp;
        n/=tmp;
    }
    return(ans);
}

int p(int m,int tmp)
{
    int ans=0,n=m;
    while(n/2)
    {
        ans+=n/10+((n)>=tmp);
        int txt=n/5;
        while(txt)
        {
            ans+=txt/10+((txt)>=tmp);
            txt/=5;
        }
        n/=2;
    }
    return(ans);
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(c,0,sizeof(c));
        c[2]=cal(n,2)-cal(n-m,2);
        c[5]=cal(n,5)-cal(n-m,5);
        c[3]=p(n,3)-p(n-m,3);
        c[7]=p(n,7)-p(n-m,7);
        c[9]=p(n,9)-p(n-m,9);
        if(c[2]>c[5])
        {
            c[2]-=c[5];
            c[5]=0;
        }
        else
        {
            c[5]-=c[2];
            c[2]=0;
        }
        c[5]=c[5]>0;
        c[3]%=4;
        c[9]%=2;
        c[7]%=4;
        if(c[2])
        {
            if(c[2]%4)c[2]%=4;
            elsec[2]=4;
        }
        int ans=1;
        for(int i=1;i<=9;i++)
        for(int j=1;j<=c[i];j++)
        {
            ans*=i;
            ans%=10;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值