1049. Brave Balloonists
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Ten mathematicians are flying on a balloon over the Pacific ocean. When they are crossing the equator they decide to celebrate this event and open a bottle of champagne. Unfortunately, the cork makes a hole in the balloon. Hydrogen is leaking out and the balloon is descending now. Soon it will fall into the ocean and all the balloonists will be eaten by hungry sharks.
But not everything is lost yet. One of the balloonists can sacrifice himself jumping out, so that his friends would live a little longer. Only one problem still exists: who is the one to get out. There is a fair way to solve this problem. First, each of them writes an integer
ai not less than 1 and not more than 10000. Then they calculate the magic number
N that is the number of positive divisors of the product
a
1*
a
2*…*
a
10. For example, the number of positive integer divisors of 6 is 4 (they are 1,2,3,6). The hero (a mathematician who will be thrown out) is determined according to the last digit of
N. Your task is to find this digit.
Input
Input contains ten integer numbers (each number is in separate line).
Output
Output a single digit from 0 to 9 — the last digit of
N.
Sample
input | output |
---|---|
1 2 6 1 3 1 1 1 1 1 | 9 |
题意:
给出10个数,求出这10个数的积 M,然后计算 M 的因子个数 N ,然后输出 N%10;
百度求因子个数的高效算法时候看到酱紫一个定理,刚好用到:
对于一个数n,将n进行素因子分解:n=p1^a1*p2^a2*p3^a3*...pn^an则其因子的个数为(a1+1)*(a2+1)*(a3+1)*...*(an+1);
标准化一些这叫约数个数定理,详见这里http://baike.baidu.com/view/1780622.htm?fr=aladdin
解题思路就是把每个非1的数先分解质因数,记录下质因数的幂,然后利用定理求。
//基础数论题,给出10个数,求出这10个数的积 M,然后计算 M 的因子个数 N ,然后输出 N%10;
// 直接累乘肯定会爆掉 如何不改变因子个数的情况下取余 ?
//
//看到一个结论:对于一个数n,将n进行素因子分解:n=p1^a1*p2^a2*p3^a3*...pn^an则其因子的个数为(a1+1)*(a2+1)*(a3+1)*...*(an+1);
//根据这个结论,可以将所有数都分解质因数,然后用这个结论处理。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 10005
int prime[maxn];
int a[11];
bool pri(int n)
{
for(int i=2;i<=sqrt(1.0*n);i++)
if(n%i==0)return false;
return true;
}
int p(int n)
{
int i,j=n;
for(i=2;i<n;i++)
{
if(pri(i))
{
while(n%i==0)
{
n/=i;
prime[i]++;
}
}
}
if(j%i==0)prime[i]++;
}
int main()
{
// freopen("q.in","r",stdin);
int i,j;
memset(prime,0,sizeof(prime));
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(a[i]!=1)
p(a[i]);
}
// cout<<prime[2]<<" "<<prime[3]<<endl;
int ans=1;
for(i=0;i<maxn;i++)
{
if(prime[i])
{
ans*=(prime[i]+1);
ans%=10;
}
}
cout<<ans%10<<endl;
}