hdu1299 素因子分解

传送门

题意:求使不定方程1 / x + 1 / y = 1 / n成立的正整数解。

思路:x、y一定是大于n的数,设y=n+k,则1/x=1/n-1/(n+k),整理一下即x=n^2/k+n,所以只要k是n^2的因子,

就一定有解,那么就转化成了n^2的因子个数。直接求n^2的因子个数容易超时,转化成n来做,

若n=a1^x1+a2^x2+a3^x3+……(a1,a2,a3……都是素因子)则n^2的因子个数为(1+2x1)(1+2x2)(1+2x3)……

(n的因子的个数为(1+x1)(1+x2)(1+x3)……)

最后不要忘记加1除2。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e6+5;
bool temp[N];
int data[N/10];
int cnt=0;
void prime()
{
    temp[2]=true;
    for(int i=3;i<N;i+=2)
    {
        temp[i]=true;
        temp[i+1]=false;
    }
    for(int i=3;i<N;i+=2)
        if(temp[i])
            for(int j=i+i;j<N;j+=i) temp[j]=false;
    for(int i=2;i<N;i++)
    {
        if(temp[i]) data[cnt++]=i;
    }
}
ll fun(ll a)
{
    ll ans=1;
    for(int i=0;i<cnt&&data[i]*data[i]<=a;i++)
    {
        int c=0;
        while(a%data[i]==0)
        {
            c++;
            a/=data[i];
        }
        ans*=(2*c+1);
    }
    if(a>1) ans*=3;
    ans=(ans+1)/2;
    return ans;
}
int main()
{
    prime();
    int t;
    scanf("%d",&t);
    int k=0;
    while(t--)
    {
        k++;
        ll n;
        scanf("%lld",&n);
        printf("Scenario #%d:\n%lld\n\n",k,fun(n));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值