結果

問題 No.1085 桁和の桁和
ユーザー furafura
提出日時 2020-06-19 22:23:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,078 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 196,300 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2024-04-24 10:05:34
合計ジャッジ時間 3,039 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,168 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 4 ms
7,296 KB
testcase_03 AC 3 ms
7,168 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 4 ms
7,296 KB
testcase_08 AC 3 ms
7,040 KB
testcase_09 AC 3 ms
7,168 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 3 ms
7,168 KB
testcase_12 AC 4 ms
7,168 KB
testcase_13 AC 5 ms
7,040 KB
testcase_14 AC 8 ms
7,040 KB
testcase_15 AC 12 ms
7,424 KB
testcase_16 AC 11 ms
7,168 KB
testcase_17 AC 8 ms
7,040 KB
testcase_18 AC 6 ms
7,040 KB
testcase_19 AC 10 ms
7,168 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 8 ms
7,168 KB
testcase_22 AC 10 ms
7,296 KB
testcase_23 AC 5 ms
7,040 KB
testcase_24 AC 4 ms
7,168 KB
testcase_25 AC 8 ms
7,168 KB
testcase_26 AC 12 ms
7,296 KB
testcase_27 AC 11 ms
7,296 KB
testcase_28 AC 12 ms
7,168 KB
testcase_29 AC 8 ms
7,168 KB
testcase_30 AC 8 ms
7,040 KB
testcase_31 AC 12 ms
7,424 KB
testcase_32 AC 10 ms
7,168 KB
testcase_33 AC 19 ms
7,296 KB
testcase_34 AC 19 ms
7,296 KB
testcase_35 AC 19 ms
7,168 KB
testcase_36 AC 19 ms
7,296 KB
testcase_37 AC 19 ms
7,296 KB
testcase_38 AC 19 ms
7,296 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[100001][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"),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