ural 1586. Threeprime Numbers

來源:互聯網
上載者:User

題意:

定義這樣一種數:Threeprime,指對於它的任意連續3位上的數字,都構成一個3位的質數。
求對於一個n位元,存在多少個Threeprime數。
讀入一行,一個整數n(3<=n<=10000)。
輸出一行,即總數mod 10^9+9。

解法:

記錄[100, 1000)範圍內的所有的素數(是素數的每一位)。然後從n = 4往後,定義dp[i][x2][x3], i表示到第i位時,第i-1位為x2, 第i為為x3,此時所包含的情況數。所以有:
dp[i][x2][x3] = (dp[i][x2][x3] + dp[i-1][x1][x2])%MOD;

最後求和sum(dp[n][x2][x3]);

#include <stdio.h>#include <stdlib.h>#include <math.h>#define Abs(x) ((x)>0?(x):-(x))#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define INF 1000000000 #define MAX_N 10010#define MOD 1000000009 bool prime[10][10][10];int f[MAX_N][10][10];void InitPrime(){prime[0][0][2]=1;int i,j;bool o;for(i=3;i<=999;i++){o=1;for(j=2;j<=sqrt(double(i))+1;j++)if(i%j==0){o=0;break;}prime[i/100][(i/10)%10][i%10]=o;}} int n;int main(){int i,j,l,k;#ifndef ONLINE_JUDGEfreopen("1586.in","r",stdin);freopen("1586.out","w",stdout);#endifInitPrime();scanf("%d",&n);for(i=1;i<=9;i++)for(j=0;j<=9;j++)for(l=0;l<=9;l++)if(prime[i][j][l])f[3][i][j]+=1;for(k=4;k<=n;k++)for(i=1;i<=9;i++)for(j=0;j<=9;j++)for(l=0;l<=9;l++)if(prime[i][j][l])f[k][i][j]=( f[k][i][j]+f[k-1][j][l] )%MOD;int ans=0;for(i=1;i<=9;i++)for(j=0;j<=9;j++)ans=(ans+f[n][i][j])%MOD;printf("%d\n",ans);return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.