結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishi
提出日時 2015-12-01 03:44:00
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,502 ms / 2,000 ms
コード長 2,472 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 105,728 KB
実行使用メモリ 20,096 KB
最終ジャッジ日時 2024-09-14 06:28:08
合計ジャッジ時間 20,783 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Solver
{
	private const long MOD = 1000000007;

	private long calc(String n, int p){
		long[,,] dp = new long[2,800,2*4*2];
		int u = 0;
		int v = 1;
		dp[0,0,0] = 1;

		for(int i=0; i<n.Length; i++){
			int k = ((int)n[i]) - ((int)'0');
			if(i<n.Length-5){
				for(int a=0;a<2;a++){
				for(int b=0;b<3;b++){
				for(int c=0;c<2;c++){
					int s = (a<<3)|(b<<1)|c;
					dp[v,0,s] = 0;
				}}}

				for(int a=0;a<2;a++){
				for(int b=0;b<3;b++){
				for(int c=0;c<2;c++){
					int s = (a<<3)|(b<<1)|c;
					for(int d=0;d<10;d++){
						if(a==0 && d>k){
							continue;
						}
						int a_ = a|(d<k?1:0);
						int b_ = (b+d)%3;
						int c_ = c|(d==3?1:0);
						int s_ = (a_<<3)|(b_<<1)|c_;

						dp[v,0,s_] += dp[u,0,s];
						if(dp[v,0,s_] >= MOD) dp[v,0,s_] -= MOD;
					}
				}}}
			}
			else{
				for(int j=0;j<p;j++){
				for(int a=0;a<2;a++){
				for(int b=0;b<3;b++){
				for(int c=0;c<2;c++){
					int s = (a<<3)|(b<<1)|c;
					dp[v,j,s] = 0;
				}}}}

				for(int j=0;j<p;j++){
				for(int a=0;a<2;a++){
				for(int b=0;b<3;b++){
				for(int c=0;c<2;c++){
					int s = (a<<3)|(b<<1)|c;
					for(int d=0;d<10;d++){
						if(a==0 && d>k){
							continue;
						}
						int a_ = a|(d<k?1:0);
						int b_ = (b+d)%3;
						int c_ = c|(d==3?1:0);
						int s_ = (a_<<3)|(b_<<1)|c_;
						int j_ = (j*10 + d) % p;
						dp[v,j_,s_] += dp[u,j,s];
						if(dp[v,j_,s_] >= MOD) dp[v,j_,s_] -= MOD;
					}
				}}}}
			}
			u ^= 1;
			v ^= 1;
		}

		long ret = 0;

		for(int j=1;j<p;j++){
		for(int a=0;a<2;a++){
		for(int b=0;b<3;b++){
		for(int c=0;c<2;c++){
			if(c==0 && b!=0){
				continue;
			}
			int s = (a<<3)|(b<<1)|c;
			ret += dp[u,j,s];
			if(ret>=MOD) ret -= MOD;
		}}}}

		return ret;
	}
    public void Solve()
    {
        String[] input = Console.ReadLine().Split();
        int p = int.Parse(input[2]);
        long ans = calc(input[1], p);
        ans = (ans - calc(input[0], p) + MOD) % MOD;

        int b=0;
        int c=0;
        int d=0;
        for(int i=0;i<input[0].Length; i++){
			int k = ((int)input[0][i]) - ((int)'0');
			b = (b+k)%3;
			c = c|(k==3?1:0);
			d = (d*10+k)%p;
        }
        if((b==0 || c==1) && d != 0){
        	ans = (ans+1)%MOD;
        }

        Console.WriteLine(ans);

    }
    static int Main()
    {
        var solver = new Solver();
        solver.Solve();
        return 0;
    }
}
0