結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-07 23:43:13
言語 Go
(1.22.1)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 2,099 bytes
コンパイル時間 13,812 ms
コンパイル使用メモリ 220,080 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 03:53:17
合計ジャッジ時間 16,820 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 13 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 15 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 76 ms
5,376 KB
testcase_13 AC 74 ms
5,376 KB
testcase_14 AC 155 ms
5,376 KB
testcase_15 AC 154 ms
5,376 KB
testcase_16 AC 152 ms
5,376 KB
testcase_17 AC 154 ms
5,376 KB
testcase_18 AC 79 ms
5,376 KB
testcase_19 AC 79 ms
5,376 KB
testcase_20 AC 90 ms
5,376 KB
testcase_21 AC 92 ms
5,376 KB
testcase_22 AC 165 ms
5,376 KB
testcase_23 AC 165 ms
5,376 KB
testcase_24 AC 155 ms
5,376 KB
testcase_25 AC 165 ms
5,376 KB
testcase_26 AC 155 ms
5,376 KB
testcase_27 AC 154 ms
5,376 KB
testcase_28 AC 145 ms
5,376 KB
testcase_29 AC 270 ms
5,376 KB
testcase_30 AC 256 ms
5,376 KB
testcase_31 AC 256 ms
5,376 KB
testcase_32 AC 315 ms
5,376 KB
testcase_33 AC 165 ms
5,376 KB
testcase_34 AC 214 ms
5,376 KB
testcase_35 AC 340 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	"strings"
)

//var sc = bufio.NewScanner(os.Stdin);
var rd = bufio.NewReaderSize(os.Stdin, 1000000);
/*
func nextInt() int{
	sc.Scan();
	ret,_ := strconv.Atoi(sc.Text());
	return ret;
}

func nextStr() string{
	sc.Scan();
	ret := sc.Text();
	return ret;
}
*/

var mod int64 = 1000000007;

func calc(x *string, p int) int64{
	var dp  [2][800][16]int64;
	dp[0][0][0] = 1;
	var sz int = len(*x); 
	for i :=0; i<sz; i++ {
		k := int((*x)[i] - '0');
		mod8 := p;
		if(i<sz-5){
			mod8 = 1;
		}
		for j:=0; j<mod8; j++{
			for a:=0; a<2; a++ {
				for b:=0; b<3; b++ {
					for c:=0; c<2; c++ {
						var s int = (a<<3)|(b<<1)|c;
						var val int64 = dp[i&1][j][s];
						dp[i&1][j][s] = 0;
						for d:=0; d<10; d++ {
							if(a==0 && d>k){
								break;
							}
							var a_ int = a;
							if(d<k){
								a_ |= 1;
							}
							var b_ int = (b+d)%3;
							var c_ int = c;
							if(d==3){
								c_ |= 1;
							}
							var s_ int = (a_<<3)|(b_<<1)|c_;
							var j_ int = 0;
							if( i>=sz-5 ){
								j_ = (j*10+d)%p;
							}
							dp[(i&1)^1][j_][s_] += val;
							if(dp[(i&1)^1][j_][s_] >= mod){
								dp[(i&1)^1][j_][s_] -= mod;
							}
						}
					}
				}
			}
		}
	}

	var ret int64 = 0;
	for j:=1; j<p; j++ {
		for a:=0; a<2; a++ {
			for b:=0; b<3; b++ {
				for c:=0; c<2; c++ {
					if(b!=0 && c==0){
						continue;
					}
					s:=(a<<3)|(b<<1)|c;
					ret += dp[sz&1][j][s];
					if(ret >= mod){
						ret -= mod;
					}
				}
			}
		}
	}
	return ret;
}

func main(){
	//sc.Split(bufio.ScanWords);
	/*
	var a string = nextStr();
	var b string = nextStr();
	var p int = nextInt();
	*/
	in,_,_ := rd.ReadLine();
	tmp := strings.Split(string(in), " ");
	var a, b string = tmp[0], tmp[1];
	p,_ := strconv.Atoi(tmp[2]);

	var ans int64 = 0;
	ans += calc(&b, p);
	ans -= calc(&a, p);
	ans = (ans+mod)%mod;

	var x,y,z int = 0,0,0;
	for i:=0; i<len(a); i++ {
		k := int(a[i] - '0');
		x = (x+k)%3;
		if(k==3){
			y = 1;
		}
		z = (z*10+k)%p;
	}
	if((x==0||y==1)&&z!=0){
		ans++;
		ans %= mod;
	}
	fmt.Println(ans);

}
0