1. 程式人生 > >[簡單題] Project Euler 603 Substring sums of prime concatenations

[簡單題] Project Euler 603 Substring sums of prime concatenations

直接考慮每一位的貢獻,應該是一個 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 603 Substring sums of prime concatenations

直接考慮每一位的貢獻,應該是一個 ai×i×(1+10+⋯+10n−i) 的形式,這就是個等比數列求和 然後因為是迴圈串,還是個等比數列求和,就好了 #include<cstdio>

Project Euler】530 GCD of Divisors 莫比烏斯反演

rac 困難 efi pan opened can tps blank 分塊 【題目】GCD of Divisors 【題意】給定f(n)=Σd|n gcd(d,n/d)的前綴和F(n),n=10^15。 【算法】莫比烏斯反演 【題解】參考:任之洲數論函數.pdf 這個範圍

Project Euler 31- Coin sums

題目描述 有面值分別為1,2,5,10,20,50,100,200的硬幣,現要湊齊200的面值,請問一共有多少種湊法。 問題分析 不正確的解法 直接想到的方法是使用動規: 狀態轉換方程:f(x) = sum { f(x - a[i]) }

Project Euler:Problem 41 Pandigital prime

emp ont div track using string space number end We shall say that an n-digit number is pandigital if it makes use of all the digits

[Project Euler 429] Sum of squares of unitary divisors(數論)

blog ont namespace href 題解 str return void rap 題目鏈接:https://projecteuler.net/problem=429 題目: A unitary divisor dd of a number nn is a d

Project Euler第一階段刷總結 • cenalulu's Tech Blog

本文是博主對於用Python進行Project Euler第一階段刷題的總結和一些感悟 背景:為了更好的學習Python,博主從2015-01-01開始進行Project Euler的刷題。至今已經完成了兩個階段(50題)。下面是兩階段以來的一些經驗和感悟 Project E

Project Euler #1: Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find th

[BestCoder Round #3] hdu 4907 Task schedule (模擬簡單

using mes family set key problem 占用 時間 task Task schedule Problem Description 有一臺機器,而且給你這臺機器的工作表。工作表上有n個任務,機器在ti時間運行第i個任務,1秒就可以完畢

【插頭dp】CDOJ1690 這是一道比CCCC簡單難的簡單

eof pri mes main 一道 sizeof () brush mage 最裸的插頭dp,可參見大白書。 #include<cstdio> #include<cstring> using namespace std; #define

uva 10474 Where is the Marble?(簡單

content mil stdlib.h std lib [0 數據 main pre 我非常奇怪為什麽要把它歸類到回溯上,明明就是簡單排序,查找就OK了。wa了兩次,我還非常不解的懷疑了為什麽會 wa,原來是我居然把要找的數字也排序了,當時僅僅是想著能快一點查找。所以

project euler 14 collatz

xiang wan urb col site asi pri hang dai Java%E5%AD%97%E8%8A%82%E3%80%81%E5%8D%81%E8%BF%9B%E5%88%B6%E3%80%81%E5%8D%81%E5%85%AD%E8%BF%9B%E5

bzoj3687簡單(dp+bitset優化)

eve clas define string onclick discuss class www content 3687: 簡單題 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 861 Solved: 399[Su

hdoj 5092 Seam Carving 【樹塔DP變形 + 路徑輸出】 【簡單

mes class mar constant it is tor char 題意 esp Seam Carving Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J

HDU 1846 Brave Game(巴什博弈超簡單

sca space == div 法則 由於 out con 希望 題目傳送:http://acm.hdu.edu.cn/showproblem.php?pid=1846 Problem Description 十年前讀大學的時候,中國每年都要從國外引進一些電影大片,其中有

Project Euler:problem 104

clu int hub printf div pro key urn 鏈接 題目鏈接:http://pe-cn.github.io/104/ # include <stdio.h> # include <stdlib.h> void sort(i

解決maven web項目Cannot detect Web Project version. Please specify version of Web Project through...的錯誤

version 錯誤 web http conf mpi int war targe 前面已經聊了創建maven web工程,但是問題來了,創建maven web工程之後會出現如下的錯誤,在pom.xml文件頭部 有以下的錯誤 [plain] view plain

Project Euler:Problem 34 Digit factorials

class -a post ria urn back ble while [1] 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the s

BZOJ 4066 簡單(KD樹)

element space while rebuild 鏈接 main query [1] line 【題目鏈接】 http://www.lydsy.com/JudgeOnline/problem.php?id=4066 【題目大意】   要求維護矩陣內格子

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet

spc tid int exists post auto gsp none style [Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet Description A Pythagorea

【Luogu】 P3928 SAC E#1 - 一道簡單 Sequence2

lap sed fin bit amp lowbit pac first main 【題目】洛谷10月月賽R1 提高組 【算法】遞推DP+樹狀數組 【題解】列出DP遞推方程,然後用樹狀數組維護前後綴和。 #include<cstdio> #include&l