結果
問題 |
No.315 世界のなんとか3.5
|
ユーザー |
![]() |
提出日時 | 2015-12-08 14:22:24 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 596 ms / 2,000 ms |
コード長 | 2,708 bytes |
コンパイル時間 | 2,276 ms |
コンパイル使用メモリ | 77,480 KB |
実行使用メモリ | 57,028 KB |
最終ジャッジ日時 | 2024-09-14 20:09:53 |
合計ジャッジ時間 | 15,574 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 36 |
ソースコード
package no315; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { public static long MOD = 1000000007; public static long stime; public static void main(String[] args) throws FileNotFoundException { Scanner sc = new Scanner(System.in); //Scanner sc = new Scanner(new File("in.txt")); stime = System.nanoTime(); String a = sc.next(); String b = sc.next(); int zeros = sc.next().length() - 1; long bb = aho(b,zeros); char[] as = a.toCharArray(); for(int i=as.length-1;i>=0;i--) { if (as[i] >= '1') { as[i]--; break; } as[i] = '9'; } long aa = aho(String.valueOf(as),zeros); System.out.println((bb + MOD - aa) % MOD); } public static void lap() { System.out.println((System.nanoTime() - stime) / 1000000); } public static boolean useNaive = false; public static long aho(String ss,int zeros) { if (useNaive && ss.length() <= 5) { return naiveAho(Integer.parseInt(ss), zeros); } char[] s = ss.toCharArray(); int n = Math.max(0,s.length - 3 - zeros); int[][][] dp = new int[2][4][n+1]; dp[1][0][0] = 1; for(int i=0;i<n;i++) { for(int large=0;large<2;large++) { for(int j=0;j<4;j++) { int max = large == 0 ? 9 : s[i] - '0'; for(int k=0;k<=max;k++) { int nlarge = large & (k == max ? 1 : 0); int nj = (j == 3 || k == 3) ? 3 : (j + k) % 3; dp[nlarge][nj][i+1] += dp[large][j][i]; if (dp[nlarge][nj][i+1] >= MOD) { dp[nlarge][nj][i+1] -= MOD; } } } } } long ans = 0; int max = 1000; int step = 8; for(int i=0;i<zeros;i++) { max *= 10; step *= 10; } int tail = Integer.parseInt(ss.substring(n, ss.length())); for(int i=0;i<max;i++) { if (i % step == 0) { continue; } int sum = 0; boolean three = false; int x = i; while(x > 0) { int d = x % 10; sum += d; if (d == 3) { three = true; break; } x /= 10; } for(int large=0;large<2;large++) { if (large == 1 && i > tail) { continue; } for(int j=0;j<4;j++) { if (j == 3 || three || (j+sum)%3==0) { if (dp[large][j][n] > 0) { // System.out.println(i + "," + large + "," + j + "," + n); } ans += dp[large][j][n]; } } } } return ans % MOD; } public static long naiveAho(int max,int zeros) { long ans = 0; int step = 8; for(int i=0;i<zeros;i++) { step *= 10; } for(int i=0;i<=max;i++) { if (i % step == 0) { continue; } int x = i; boolean three = false; while(x > 0) { int d = x % 10; if (d == 3) { three = true; break; } x /= 10; } if (i % 3 == 0 || three) { ans++; } } return ans; } }