URAL 1049

来源:互联网 发布:qq空间源码 编辑:程序博客网 时间:2024/06/05 21:08

题目大意:输出10个正整数乘积的因子个数的末尾数字。

Time Limit:2000MS     Memory Limit:16384KB     64bit IO Format:%I64d & %I64u

规模:1<=ai<=10000(i=0,1,2,...,10);

理论基础:

定理:如果一个数N=p1^k1*p2^k2*...(p1,p2,...均为素数)。

那么:它的因子数:cnt=(k1+1)*(k2+1)*...

题目分析:因为N=a1*a2*...的形式,所以问题转化为将每个因子ai分解质因数,并统计每个素数的个数的问题。变得非常简单。

代码如下

#include<iostream>#include<cstring>#include<string>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>using namespace std;typedef double db;#define maa (1<<31)#define mii ((1<<31)-1)#define DBG 0#define sl(c) ((int)(c).size())    //取字符串长度;#define forl(i, a, b) for(int i = (a); i <  (b); ++i)    //不带边界值循环,正序#define forle(i, a, b) for(int i = (a); i <= (b); ++i)   //带边界值循环,正序#define forh(i, a, b) for(int i = (a); i >  (b); --i)     //不带边界值,逆序#define forhe(i, a, b) for(int i = (a); i >= (b); --i)        //带边界值,逆序#define forlc(i, a, b) for(int i##_b = (b), i = (a); i <  i##_b; ++i)  //带别名的循环,用于不可改变值#define forlec(i, a, b) for(int i##_b = (b), i = (a); i <= i##_b; ++i)#define forgc(i, a, b) for(int i##_b = (b), i = (a); i >  i##_b; --i)#define forgec(i, a, b) for(int i##_b = (b), i = (a); i >= i##_b; --i)#define forall(i, v   )  forl(i, 0, sz(v))   //循环所有#define forallc(i, v   ) forlc(i, 0, sz(v))#define forlla(i, v   ) forhe(i, sz(v)-1, 0)#define forls(i, n, a, b) for(int i = a; i != b; i = n[i])   //搜表用#define rep(n)  for(int               repp = 0; repp <    (n); ++repp)#define repc(n) for(int repp_b = (n), repp = 0; repp < repp_b; ++repp)#define rst(a, v) memset(a, v, sizeof a)   //把字符v填充到a  reset 重置#define cpy(a, b) memcpy(a, b, sizeof a)   //copy b 的sizeof(a)个字符到a#define rstn(a, v, n) memset(a, v, (n)*sizeof((a)[0]))  //把字符v填充到a[n]之前的字节#define cpyn(a, b, n) memcpy(a, b, (n)*sizeof((a)[0]))    //copy b 的 n 个字符到a#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); }  //调试#define dout DBG && cout << __LINE__ << ">>| "#define pr(x) #x"=" << (x) << " | "#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl#define pra(arr, a, b)  if(DBG) {\    dout<<#arr"[] |" <<endl; \    forlec(i, a, b) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \    if((b-a+1)%8) puts("");\}                                                             //数列查看#define rd(type, x) type x; cin >> x   //读数inline int     rdi() { int d; scanf("%d", &d); return d; }inline char    rdc() { scanf(" "); return getchar(); }inline string  rds() { rd(string, s); return s; }inline db rddb() { db d; scanf("%lf", &d); return d; }template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }int number[10000]={0};bool isprime(int i){    forl(j,2,(int)sqrt((float)i))if(i%j==0)return false;    return true;}int main(){    int ma=maa;    forl(i,0,10)    {        int n=rdi();        int t=n;        ma=max(ma,n);        forle(j,2,n)        {            while(isprime(j)&&t%j==0)            {                number[j]++;                t/=j;            }        }    }    int total=1;    forle(j,2,ma)    {        if(number[j])total*=(number[j]+1);    }    printf("%d\n",total%10);return 0;}
当然,预处理素数可以提高效率,对于此题,完全没有必要。


参考文献:http://baike.baidu.com/view/1780622.htm

by : Jsun_moon  http://blog.csdn.net/Jsun_moon

原创粉丝点击