码迷,mamicode.com
首页 > 其他好文 > 详细

Large repunit factors (PE 132)

时间:2017-04-26 10:04:31      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:efi   power   cst   alt   clu   string   unit   algo   code   

题目大意:

求出 大数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 然后检验是否满足这个同余式就好了。

 

代码:

技术分享
#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;
}
View Code

 答案843296

Large repunit factors (PE 132)

标签:efi   power   cst   alt   clu   string   unit   algo   code   

原文地址:http://www.cnblogs.com/vb4896/p/6766909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!