結果

問題 No.1086 桁和の桁和2
ユーザー furafura
提出日時 2020-06-20 00:08:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,985 bytes
コンパイル時間 2,723 ms
コンパイル使用メモリ 205,232 KB
実行使用メモリ 5,140 KB
最終ジャッジ日時 2023-09-16 16:13:17
合計ジャッジ時間 6,287 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 43 ms
5,000 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 28 ms
4,496 KB
testcase_11 AC 9 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 36 ms
4,660 KB
testcase_14 AC 23 ms
4,380 KB
testcase_15 AC 16 ms
4,380 KB
testcase_16 AC 87 ms
4,384 KB
testcase_17 AC 9 ms
4,380 KB
testcase_18 WA -
testcase_19 AC 99 ms
4,608 KB
testcase_20 AC 14 ms
4,376 KB
testcase_21 AC 86 ms
4,392 KB
testcase_22 AC 118 ms
5,016 KB
testcase_23 AC 72 ms
4,380 KB
testcase_24 AC 51 ms
4,380 KB
testcase_25 AC 101 ms
4,900 KB
testcase_26 AC 84 ms
4,404 KB
testcase_27 AC 59 ms
4,384 KB
testcase_28 AC 51 ms
4,380 KB
testcase_29 WA -
testcase_30 AC 120 ms
5,000 KB
testcase_31 AC 119 ms
4,872 KB
testcase_32 WA -
testcase_33 AC 125 ms
5,140 KB
testcase_34 AC 124 ms
4,892 KB
testcase_35 AC 44 ms
4,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

using namespace std;
using lint=long long;

template<class R>
class matrix{
	vector<vector<R>> a;
public:
	matrix(int n):a(n,vector<R>(n)){}
	matrix(int m,int n):a(m,vector<R>(n)){}

	matrix& operator+=(const matrix& A){
		assert(h()==A.h() && w()==A.w());
		int m=h(),n=w();
		rep(i,m) rep(j,n) (*this)[i][j]+=A[i][j];
		return *this;
	}
	matrix& operator-=(const matrix& A){
		assert(h()==A.h() && w()==A.w());
		int m=h(),n=w();
		rep(i,m) rep(j,n) (*this)[i][j]-=A[i][j];
		return *this;
	}
	matrix& operator*=(const matrix& A){
		assert(w()==A.h());
		int m=h(),n=w(),l=A.w();
		matrix B(m,l);
		rep(i,m) rep(j,l) rep(k,n) B[i][j]+=(*this)[i][k]*A[k][j];
		swap(*this,B);
		return *this;
	}
	matrix operator+(const matrix& A)const{ return matrix(*this)+=A; }
	matrix operator-(const matrix& A)const{ return matrix(*this)-=A; }
	matrix operator*(const matrix& A)const{ return matrix(*this)*=A; }
	const vector<R>& operator[](int i)const{ return a[i]; }
	vector<R>& operator[](int i){ return a[i]; }

	vector<R> operator*(const vector<R>& v)const{
		assert(w()==v.size());
		int m=h(),n=w();
		vector<R> res(m);
		rep(i,m) rep(j,n) res[i]+=(*this)[i][j]*v[j];
		return res;
	}

	int h()const{ return a.size(); }
	int w()const{ return a.empty()?0:a[0].size(); }

	static matrix identity(int n){
		matrix I(n);
		rep(i,n) I[i][i]=R{1};
		return I;
	}
};

template<class R>
matrix<R> pow(matrix<R> A,long long k){
	assert(A.h()==A.w());
	matrix<R> B=matrix<R>::identity(A.h());
	for(;k>0;k>>=1){
		if(k&1) B*=A;
		A*=A;
	}
	return B;
}

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; }

mint pow(mint m,long long k){
	mint res=1;
	for(;k>0;k>>=1,m*=m) if(k&1) res*=m;
	return res;
}

// 0 <= x < 10^n かつ (x の桁和 mod 9) = d をみたす x の個数を求める
mint f(lint n,int d){
	mint res=(pow(mint(10),n)-1)/9;
	if(d==0) res+=1;
	return res;
}

mint solve(lint l,lint r,int d){
	mint res=0;
	if(d==0) res+=1;
	// 10^l <= x < 10^r かつ (x の桁和 mod 9) = d をみたす x の個数を求める
	res+=f(r,d)-f(l,d);
	return res;
}

int main(){
	int n; scanf("%d",&n);
	vector<lint> l(n),r(n);
	rep(i,n) scanf("%lld",&l[i]);
	rep(i,n) scanf("%lld",&r[i]);
	vector<int> d(n);
	rep(i,n) scanf("%d",&d[i]);

	int pos=0;
	rep(i,n) if(d[i]==0) {
		if(i>0 && d[i-1]!=0) return puts("0"),0;
		pos=i+1;
	}
	l.erase(l.begin(),l.begin()+pos);
	r.erase(r.begin(),r.begin()+pos);
	d.erase(d.begin(),d.begin()+pos);
	n-=pos;

	if(d.empty()) return puts("1"),0;

	mint ans=1;
	rep(i,n){
		int d_pre=(i==0?0:d[i-1]);
		ans*=solve(l[i],r[i],(d[i]-d_pre+9)%9);
	}

	bool ok=false;
	rep(i,n) if(d[i]!=0 && d[i]!=9) ok=true;
	if(!ok) ans-=1;

	cout<<ans<<'\n';

	return 0;
}
0