Number of garlic

topic

5156: Garlic number

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 54 Solved: 40
[Submit][Status][Web Board]
Description

If any two adjacent digits of a number are prime numbers, we call it a garlic number. The smallest number of garlic is the two-digit number 11. Mr. Garlic wants to know how many garlics have K digits. Insert picture description here
Sample Input

5

Sample Output

372

Ideas

Create a dp array, store i bits on the abscissa, and store prime numbers ending in j on the ordinate.
Then facilitate 11-99 prime numbers according to dynamic programming. (So ​​you can also preprocess faster)


AC code

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x);
#define ll long long
const int maxn = 1010;
const int mod = 10007;
bool isPrime(int x){
    
    
    for(int i=2;i*i<=x;++i)
    if(x%i==0)return false;
    return true;
}
int ans,k;
int dp[maxn][15];
int main(){
    
    
    scanf("%d",&k);
    //初始化1-9
    for(int i=1;i<=9;++i)dp[1][i] = 1;
    
    for(int i=2;i<=k;++i)
    for(int j=1;j<=9;j++)
    for(int n=1;n<=9;++n)
    if(isPrime(10*n+j)){
    
    
        dp[i][j] += dp[i-1][n];
        dp[i][j] %= mod;
    }
    ans = 0;
    for(int i=1;i<=9;i++)if(dp[k][i]){
    
    
        ans+=dp[k][i];
        ans %= mod;
    }
    printf("%d\n",ans);
    return 0;
}



Guess you like

Origin blog.csdn.net/DAVID3A/article/details/115334579