Timus 1586. Threeprime Numbers

来源:互联网 发布:pkpm是什么软件 编辑:程序博客网 时间:2024/04/28 19:35
import java.io.*;import java.util.*;public class Main {    public static int is_primer(int a){        for(int i=2;i<=Math.sqrt(a);i++){            if(a%i==0) return 0;        }        return 1;    }    public static void main(String[] args) throws IOException  {        Scanner scan = new Scanner(System.in);        long dp[][] = new long[10001][100];        int arr[] = new int[1000];        for(int i=100;i<1000;i++){            arr[i] = is_primer(i);            dp[3][i%100]+=arr[i];        }        long t = 1000000009;        for(int i=4;i<10001;i++){            for(int j=10;j<100;j++){                if(dp[i-1][j]==0) continue;                for(int k=1;k<10;k+=2){                    int x = j*10+k;                    int temp = x%100;                    if(arr[x]==1){                        dp[i][temp]+=dp[i-1][j];                        dp[i][temp]%=t;                    }                }            }        }        int n = scan.nextInt();        long result = 0;        for(int i=0;i<100;i++){            result+=dp[n][i];            result%=t;        }        System.out.println(result);    }}

算法没什么复杂了,就是往前递推,看最后两位就好。

但是比如013这样的是不算3位素素滴。

所以不难,但是我还是A了很久。。。

老坑爹的地方是我吧100000009写成了 int t = 100000009;

然后dp里面保存的long, 貌似long 余 int的时候发生了点什么转换导致WA多次。。。。


给点数据吧,

5

374


50

313795603


10000

104715764


又水了一题~

原创粉丝点击