[简单题] Project Euler 603 Substring sums of prime concatenations

112人阅读 评论(0) 收藏 举报
分类:

直接考虑每一位的贡献,应该是一个 ai×i×(1+10++10ni) 的形式,这就是个等比数列求和
然后因为是循环串,还是个等比数列求和,就好了

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;

const int P=1e9+7;

const int maxn=1e6;
ll n,K=1e12,NN;
int num,prime[maxn+5];
bool vst[(maxn+5)*20];
void Pre(int n){
  for (int i=2;i<=n;i++){
    if (!vst[i]) prime[++num]=i;
    for (int j=1;j<=num && (ll)i*prime[j]<=n;j++){
      vst[i*prime[j]]=1;
      if (i%prime[j]==0) break;
    }
  }
}

int S[(maxn+5)*10];

ll Ans=0;

inline ll Pow(ll A,ll B){
  B=(B%(P-1)+P-1)%(P-1); A%=P;
  ll ret=1;
  for (;B;B>>=1,A=A*A%P)
    if (B&1)
      ret=ret*A%P;
  return ret;
}
inline ll Inv(ll A){
  return Pow(A,P-2);
}

inline void Calc(ll I,ll v){
  ll S;
  S=I*Pow(10,NN+1+n-I)%P+P-((K-1)*n+I)%P*Pow(10,NN+1-(K-1)*n-I)%P+n*Pow(10,NN+1-I)%P*(Pow(10,-n*(K-1))+P-1)%P*Inv(Pow(10,-n)+P-1)%P;
  S%=P;
  (S*=Inv(Pow(10,n)+P-1))%=P;
  (S+=P-(I+(K-1)*n+I)%P*(K%P)%P*Inv(2)%P)%=P;
  S=S*Inv(9)%P;
  (Ans+=S*v%P)%=P;
}

inline void Brute(){
  ll ret=0;
  for (int i=1;i<=n*K;i++){
    ll t=0;
    for (int j=i;j<=n*K;j++){
      t=(t*10+S[(j-1)%n+1])%P;
      (ret+=t)%=P;
    }
  }
  printf("%lld\n",ret);
}

int main(){
  Pre(maxn*20);
  for (int i=1;i<=maxn;i++){
    int a[11],t=prime[i]; *a=0;
    while (t) a[++*a]=t%10,t/=10;
    for (int j=*a;j;j--)
      S[++n]=a[j];
  }
  NN=n*K;
  for (int i=1;i<=n;i++)
    Calc(i,S[i]);
  printf("%lld\n",Ans);
  //Brute();
  return 0;
}
查看评论

project euler 解题

project euler 3:Any integer greater than 1 is either a prime number, or can be written as a unique p...
  • cctvzxxz1
  • cctvzxxz1
  • 2013-06-23 20:25:19
  • 3718

【Project Euler】3 第三题

 //The prime factors of 13195 are 5, 7, 13 and 29.         //What is the largest prime factor of...
  • NoMasp
  • NoMasp
  • 2015-02-07 12:21:52
  • 1171

Project Euler Problem 81-90

Project Euler 81-90Project Euler 81-90题解 [TOC]Problem 81 Path sum: two ways路径和:两个方向 在如下的5乘5矩阵中,从左上...
  • u011401504
  • u011401504
  • 2016-03-05 22:17:05
  • 815

project euler 49

Problem 49 Prime permutations The arithmetic sequence, 1487, 4817, 8147, in which each of ...
  • whuawell
  • whuawell
  • 2015-12-04 21:34:22
  • 230

各种语言版本实现欧拉猜想 Euler's sum of powers conjecture

Euler's sum of powers conjecture There is a conjecture in mathematics that held for over 200 ...
  • GarfieldEr007
  • GarfieldEr007
  • 2016-01-07 12:44:19
  • 648

Project Euler:Problem 23 Non-abundant sums

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number...
  • youb11
  • youb11
  • 2015-06-01 22:12:14
  • 672

【Project Euler】8 第八题

 A Pythagorean triplet is a set of three natural numbers, a a2 + b2 = c2 For example, 32 +...
  • NoMasp
  • NoMasp
  • 2015-02-07 12:26:00
  • 1398

Project Euler 1-5题

第1题 题目来源ProjectEuler这个题求的是严格小于1000的数中,是3或5的倍数的数的和。(刚开始理解错below的意思了,把1000算进去了,尴尬)int main(){ ...
  • pfccWang
  • pfccWang
  • 2017-09-10 21:25:29
  • 342

project Euler第八题

题目: Find the greatest product of five consecutive digits in the 1000-digit number. 7316717653133...
  • shangzhihaohao
  • shangzhihaohao
  • 2011-11-07 14:49:40
  • 600

Project Euler:Problem 8

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 =...
  • youb11
  • youb11
  • 2015-05-30 12:16:18
  • 753
    个人资料
    持之以恒
    等级:
    访问量: 39万+
    积分: 1万+
    排名: 1232
    文章分类
    最新评论