CodeForces 172 D.Calendar Reform(数论)

来源:互联网 发布:收费网站源码 编辑:程序博客网 时间:2024/05/23 21:26

Description
第一年a天,第二年a+1天,以此类推,第n年a+n-1天,一年可以被分成几个月,一个月的天数必须是完全平方数,一年中每个月的天数相同,问这n天最多有几个月
Input
两个整数a和n(1<=a,n<=1e7,a+n-1<=1e7)
Output
输出最少月数
Sample Input
25 3
Sample Output
30
Solution
问题转化为把a~a+n-1中的每个数拆成a^2*b形式,且b最小,所以把每个数的平方因子全部除掉即可
Code

#include<cstdio>#include<cstring>using namespace std;typedef long long ll;const int maxn=10000001;int a,n,x[maxn];int main(){    scanf("%d%d",&a,&n);    for(int i=a;i<=a+n-1;i++)x[i]=i;    for(int i=2;i*i<=a+n-1;i++)    {        int p=i*i;        int t=((a+p-1)/p)*p;        for(int j=t;j<=a+n-1;j+=p)            while(x[j]%p==0)x[j]/=p;    }    ll ans=0;    for(int i=a;i<=a+n-1;i++)ans+=x[i];    printf("%I64d\n",ans);    return 0;}