poj 1150 数论

来源:互联网 发布:html5仿淘宝手机商城 编辑:程序博客网 时间:2024/06/03 21:33
排列数的最后一个非零数字

一个做了好久的题目
    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;
}