Large repunit factors (Project Euler 132)
题目大意:
求出 大数111111.....1 (1e9个1) 前40个质因子的和。
思路:
可以把原来的数表示成$\frac{10^k - 1}{9}$ 其中$k=10^9$
如果一个质数$p$ 满足 $p\mid \frac{10^k - 1}{9}$
这等价于 $9p\mid\ 10^k - 1$
即$10^k \equiv\ 1\ (mod\ 9p)$
只要从小到大枚举质数p 然后检验是否满足这个同余式就好了。
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
#define N 10000000
#define M 1100
typedef pair<int,int> pii;
bool flag[N];
int p[N],phi[N];
void Get_Primes()
{
phi[1]=1;
for (int i=2;i<N;i++)
{
if (!flag[i]) p[++p[0]]=i,phi[i]=i-1;
for (int j=1;j<=p[0] && i*p[j]<N;j++)
{
flag[i*p[j]]=true;
if (i%p[j]==0)
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
else phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
}
int Power(int x,int P,int mod)
{
int res = 1;
for (; P ; P >>= 1)
{
if (P & 1) res = 1ll * res * x % mod;
x = 1ll * x * x % mod;
}
return res;
}
int main()
{
Get_Primes();
int cnt = 0; ll ans = 0;
for (int i = 1; cnt < 40; ++i) if (Power(10, 1000000000, 9 * p[i]) == 1) ++cnt, ans += p[i];
cout << ans << endl;
return 0;
}
答案843296
Every day is meaningful, keeping learning!