【原创】【数论】HDU 2462 The Luckiest number
2017-07-08 09:55
274 查看
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
110000
0
Sample Output
610
题目大意
给你一个数L,找一个全是8的数来整除它,求这个88…8最少有几个8。多组数据,以0结束。
题目分析
以下内容为图片:有一个问题需要注意,我在上面被卡了很久。
先看我的错误程序吧。
#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; } }
相关文章推荐
- POJ 3696/ HDU 2462 The Luckiest number (数论)
- hdu 2462 The Luckiest number
- hdu 2462 The Luckiest number
- The Luckiest number(hdu 2462)
- HDU 2462 The Luckiest number
- HDU 2462 The Luckiest number
- hdu 2462 poj 3696 The Luckiest number fzu 1017 zoj 1537 Playing with Calculator
- hdu 2462 数学欧拉 The Luckiest number
- HDU 2462 The Luckiest number
- hdu 2462 The Luckiest number
- HDU1492-The number of divisors(约数) about Humble Numbers-数论(计数问题)
- poj 3696 The Luckiest number——数论 (快速幂取模,乘法取模模版)
- POJ 3696 The Luckiest number(数论)
- ACM 数论 hdu 1492 The number of divisors(约数) about Humble Numbers
- POJ 3696 : The Luckiest number - 欧拉函数,快速幂[数论好题]
- poj 3696 The Luckiest number (数论-快速幂+欧拉定理)
- [POJ3696]The Luckiest number(数论)
- Poj-3696 The Luckiest number(数论)
- 【hdu2462】【数论】【欧拉函数+欧拉定理+大数快速幂】The Luckiest number
- POJ The Luckiest number 3696 数论(处理快速幂过程中超int64的情况)