Artifact Content
Not logged in

Artifact ba4c7d03121589b068425233b78e23df02f23454


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint():val(0){}
	mint(int      x):val(x%MODVAL) {}
	mint(unsigned x):val(x%MODVAL) {}
	mint(LL       x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }

mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; }

class Nine { public:
	int count(int N, vector <int> d)
	{
		const int M = d.size();

		vector<int> cnt(1<<N);
		for(int di: d)
			cnt[di]++;

		vector<vector<mint>> pat(M+1, vector<mint>(9));
		pat[0][0] = 1;
		for(int cnt=1; cnt<=M; ++cnt) {
			for(int pre=0; pre<9; ++pre)
			for(int incr=0; incr<=9; ++incr)
				pat[cnt][(pre+incr)%9] += pat[cnt-1][pre];
		}

		int TBLMAX = 1; for(int i=0; i<N; ++i) TBLMAX*=9;
		vector<mint> dp(TBLMAX);
		dp[0] = 1;

		for(int mask=0; mask<(1<<N); ++mask)
		{
			int c = cnt[mask]; // #{i | d[i] = mask}
			vector<mint> dp2(TBLMAX);

			for(int rem_state=0; rem_state<TBLMAX; ++rem_state)
			{
				for(int tgt=0; tgt<9; ++tgt)
				{
					int r2 = 0;
					for(int f=1,i=0; i<N; f*=9,++i)
					{
						if(mask&(1<<i)) {
							r2 += ((rem_state/f + tgt*c)%9)*f;
						} else {
							r2 += (rem_state/f%9)*f;
						}
					}
					dp2[r2] += dp[rem_state] * pat[c][tgt];
				}
			}

			dp = std::move(dp2);
		}
		return dp[0].val;
	}

};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, Nine().count(N, d));}
int main(){

CASE(0)
	int N = 2; 
	int d_[] = {1,2};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int _ = 4; 
END
CASE(1)
	int N = 2; 
	int d_[] = {1,2,3};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int _ = 16; 
END
CASE(2)
	int N = 1; 
	int d_[] = {0,0,1};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int _ = 200; 
END
CASE(3)
	int N = 5; 
	int d_[] = {1,3,5,8,24,22,25,21,30,2,4,0,6,7,9,11,14,13,12,15,18,17,16,19,26,29,31,28,27,10,20,23};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int _ = 450877328; 
END
CASE(4)
	int N = 5; 
	int d_[] = {31,31,31,31,31};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int _ = 11112; 
END
/*
CASE(5)
	int N = ; 
	int d_[] = ;
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int _ = ; 
END
CASE(6)
	int N = ; 
	int d_[] = ;
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int _ = ; 
END
*/
}
// END CUT HERE