結果

問題 No.1085 桁和の桁和
ユーザー furafura
提出日時 2020-06-19 22:22:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,076 bytes
コンパイル時間 1,821 ms
コンパイル使用メモリ 201,304 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2023-09-30 06:17:04
合計ジャッジ時間 3,398 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,512 KB
testcase_01 RE -
testcase_02 AC 5 ms
10,384 KB
testcase_03 AC 4 ms
10,376 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 4 ms
10,352 KB
testcase_08 AC 4 ms
10,512 KB
testcase_09 AC 5 ms
10,404 KB
testcase_10 RE -
testcase_11 AC 4 ms
10,380 KB
testcase_12 AC 3 ms
10,548 KB
testcase_13 AC 5 ms
10,428 KB
testcase_14 AC 7 ms
10,700 KB
testcase_15 AC 13 ms
10,572 KB
testcase_16 AC 12 ms
10,748 KB
testcase_17 AC 8 ms
10,540 KB
testcase_18 AC 5 ms
10,444 KB
testcase_19 AC 12 ms
10,480 KB
testcase_20 RE -
testcase_21 AC 10 ms
10,484 KB
testcase_22 AC 10 ms
10,636 KB
testcase_23 AC 5 ms
10,396 KB
testcase_24 AC 4 ms
10,384 KB
testcase_25 AC 8 ms
10,448 KB
testcase_26 AC 14 ms
10,456 KB
testcase_27 AC 11 ms
10,680 KB
testcase_28 AC 14 ms
10,492 KB
testcase_29 AC 9 ms
10,504 KB
testcase_30 AC 8 ms
10,656 KB
testcase_31 AC 13 ms
10,544 KB
testcase_32 AC 9 ms
10,528 KB
testcase_33 AC 21 ms
10,588 KB
testcase_34 AC 21 ms
10,588 KB
testcase_35 AC 22 ms
10,752 KB
testcase_36 AC 23 ms
10,512 KB
testcase_37 AC 22 ms
10,572 KB
testcase_38 AC 20 ms
10,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class mint{
	static const int MOD=1e9+7;
	int x;
public:
	mint():x(0){}
	mint(long long y){ x=y%MOD; if(x<0) x+=MOD; }

	mint& operator+=(const mint& m){ x+=m.x; if(x>=MOD) x-=MOD; return *this; }
	mint& operator-=(const mint& m){ x-=m.x; if(x<   0) x+=MOD; return *this; }
	mint& operator*=(const mint& m){ x=1LL*x*m.x%MOD; return *this; }
	mint& operator/=(const mint& m){ return *this*=inverse(m); }
	mint operator+(const mint& m)const{ return mint(*this)+=m; }
	mint operator-(const mint& m)const{ return mint(*this)-=m; }
	mint operator*(const mint& m)const{ return mint(*this)*=m; }
	mint operator/(const mint& m)const{ return mint(*this)/=m; }
	mint operator-()const{ return mint(-x); }

	friend mint inverse(const mint& m){
		int a=m.x,b=MOD,u=1,v=0;
		while(b>0){ int t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); }
		return u;
	}

	friend istream& operator>>(istream& is,mint& m){ long long t; is>>t; m=mint(t); return is; }
	friend ostream& operator<<(ostream& os,const mint& m){ return os<<m.x; }
	int to_int()const{ return x; }
};

mint operator+(long long x,const mint& m){ return mint(x)+m; }
mint operator-(long long x,const mint& m){ return mint(x)-m; }
mint operator*(long long x,const mint& m){ return mint(x)*m; }
mint operator/(long long x,const mint& m){ return mint(x)/m; }

// ???...? (n 文字) に 0 から 9 を当てはめて桁和 mod 9 を tar に等しくする方法の数を求める
mint solve(int n,int tar){
	static mint dp[200001][9];
	dp[0][0]=1;
	rep(i,n) rep(d1,9) rep(d2,10) dp[i+1][(d1+d2)%9]+=dp[i][d1];
	return dp[n][tar];
}

int main(){
	string s;
	int d; cin>>s>>d;
	int n=s.length();

	if(d==0){
		bool ok=true;
		rep(i,n) if(s[i]!='0' && s[i]!='?') ok=false;
		return puts(ok?"1":"0");
	}

	int cnt=0,root=0;
	rep(i,n){
		if(s[i]=='?') cnt++;
		else root=(root+s[i]-'0')%9;
	}

	mint ans=solve(cnt,(d-root+9)%9);
	if(d==9){
		bool ok=true;
		rep(i,n) if(s[i]!='0' && s[i]!='?') ok=false;
		if(ok) ans-=1;
	}
	cout<<ans<<'\n';

	return 0;
}
0