【原创】【数论】HDU 2462 The Luckiest number

The Luckiest number

题目描述

description

Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your job is to determine S modulo 29 (the rest of the division of S by 29).

Take X = 1 for an example. The positive integer divisors of 2004^1 are 1, 2, 3, 4, 6, 12, 167, 334, 501, 668, 1002 and 2004. Therefore S = 4704 and S modulo 29 is equal to 6.

Input

The input consists of several test cases. Each test case contains a line with the integer X (1 <= X <= 10000000).

A test case of X = 0 indicates the end of input, and should not be processed.

Output

For each test case, in a separate line, please output the result of S modulo 29.

Sample Input

1
10000
0

Sample Output

6
10

题目大意

给你一个数L,找一个全是8的数来整除它,求这个88…8最少有几个8。
多组数据,以0结束。

题目分析

以下内容为图片:

推导1
推导2

有一个问题需要注意,我在上面被卡了很久。
先看我的错误程序吧。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

long long r[1234321],cnt;
long long L,ans,x,k;

long long phi(long long k)
{
    long long fly=k;
    for(long long i=2;i*i<=k;i++)
        if(k%i==0)
        {
            while(k%i==0) k/=i;
            fly=fly/i*(i-1);
        }
    if(k>1) fly=fly/k*(k-1);
    return fly;
}

long long ksm(long long a,long long b,long long c)
{
    long long ans=1;
    a%=c;
    while(b)
    {
        if(b&1) ans=ans%c*a;
        b>>=1; a=a%c*a;
    }
    return ans;
}

long long gcd(long long a,long long b)
{
    return b?gcd(b,a%b):a;
}

long long again(long long k)
{
    cnt=0;
    for(long long i=1;i*i<=k;i++)
        if(k%i==0) 
        {
            r[++cnt]=i;
            if(i*i!=k) r[++cnt]=k/i;
        }
    sort(r+1,r+1+cnt);
    for(int i=1;i<=cnt;i++)
        if(ksm(10,r[i],x)==1) 
            return r[i];
    return k;
}

int main()
{
    int ss=0;
    while(cin>>L&&L)
    {
        printf("Case %d: ",++ss);
        if(L%16==0 || L%5==0) 
        {
            printf("0\n");
            continue;
        }
        x=9*L/gcd(8,L);
        k=phi(x);
        cout<<again(k)<<endl;
    }
}

当输入199999999时,几个变量是这样的:
L=1999999999
x=17999999991//(9*L)/gcd(8,L)
k=11612903040//phi(k)
以上都没问题,看最后的结果,
ans=11612903040,这个结果是不对的。
为什么?
那肯定是快速幂的问题了。

long long ksm(long long a,long long b,long long c)
{
    long long ans=1;
    a%=c;
    while(b)
    {
        if(b&1) ans=ans%c*a;
        b>>=1; 
        a=a%c*a;//←就在这里
    }
    return ans;
}

就在那里,c是x=17999999991,a模了17999999991,结果必定小于17999999991。一个小于17999999991的数,再平方,结果仍然会爆long long(最多可以爆出三亿亿)。
这就是我WA了一万次的原因。

怎么办呢?
我们只能改乘为加,牺牲时间,换取AC。

详见代码

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

long long r[1234321],cnt;
long long L,ans,x,k;

long long phi(long long k)
{
    long long fly=k;
    for(long long i=2;i*i<=k;i++)
        if(k%i==0)
        {
            while(k%i==0) k/=i;
            fly=fly/i*(i-1);
        }
    if(k>1) fly=fly/k*(k-1);
    return fly;
}

long long mul(long long a,long long b)
{
    long long ans=0;
    while(b)
    {
        if(b&1) ans=(ans+a)%x;
        b>>=1; a=(a<<1)%x;
    }
    return ans;
}
long long msm(long long a,long long b,long long c)
{
    if(b==0) return 1;
    if(b==1) return a%c;
    long long ans=msm(a,b/2,c)%c;
    ans=mul(ans,ans)%c;
    if(b%2) ans=mul(ans,a)%c;
    return ans%c;
}

long long gcd(long long a,long long b)
{
    return b?gcd(b,a%b):a;
}

long long again(long long k)
{
    cnt=0;
    for(long long i=1;i*i<=k;i++)
        if(k%i==0) 
        {
            r[++cnt]=i;
            if(i*i!=k) r[++cnt]=k/i;
        }
    sort(r+1,r+1+cnt);
    for(int i=1;i<=cnt;i++)
        if(msm(10,r[i],x)==1) 
            return r[i];
    return k;
}

int main()
{
    int ss=0;
    while(cin>>L&&L)
    {
        printf("Case %d: ",++ss);
        if(L%16==0 || L%5==0) 
        {
            printf("0\n");
            continue;
        }
        x=9*L/gcd(8,L);
        k=phi(x);
        cout<<again(k)<<endl;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值