QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#589127#8211. Enumerating Substrings369PaiAC ✓15ms9268kbC++204.9kb2024-09-25 16:16:122024-09-25 16:16:12

Judging History

你现在查看的是最新测评结果

  • [2024-09-25 16:16:12]
  • 评测
  • 测评结果:AC
  • 用时:15ms
  • 内存:9268kb
  • [2024-09-25 16:16:12]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5 , MOD = 1e9 + 7 , INV2 = MOD / 2 + 1;
int n , m , k , pw[N] , ipw[N];
namespace BF
{
	const int N = 10;
	int s1[N] , s2[N] , cnt[N] , nxt[N] , res[N];
	int Solve()
	{
		int ans = 0;
		for(int t = 0 ; t < pw[m] ; t++)
		{
			memset(cnt , 0 , sizeof cnt);
			bool fail = 0;
			for(int i = 0 ; i < m ; i++)
			{
				s2[i] = t / pw[i] % k;
				if(++cnt[s2[i]] > 2)fail = 1;
			}
			if(!fail)
			{

				// for(int i = 0 ; i < m ; i++)
				// 	cerr << s2[i] << ',';
				// cerr << "\n";
				int border = 0;
				for(int i = 1 ; i <= m / 2 ; i++)
				{
					bool ok = 1;
					for(int j = 0 ; j < i && ok ; j++)
						if(s2[j] != s2[m  - i + j])ok = 0;
					if(ok)border = i;
				}
				// cerr << "border = " << border << "\n";
				for(int s = 0 ; s < pw[n] ; s++)
				{
					for(int i = 0 ; i < n ; i++)
						s1[i] = s / pw[i] % k;
					for(int i = 0 ; i <= n - m ; i++)
					{
						bool ok = 1;
						for(int j = 0 ; j < m && ok ; j++)
							if(s1[i + j] != s2[j])ok = 0;
						if(ok)
						{
							ans++ , res[border]++;
							// cerr << "\t" << i << "  ";
							// for(int i = 0 ; i < n ; i++)
							// 	cerr << s1[i] << ',';
							// cerr << "\n";
							i += m - 1;
						}
					}
				}
			}
		}
		cout << ans << "\n";
		// for(int i = 0 ; i <= m / 2 ; i++)
		// 	cerr << res[i] << " \n"[i == m / 2];
		return 0;
	}
}
namespace NM
{
	const int M = 2005;
	int fac[M] , ifac[M] , ip2[M] , dk[M];
	int Qpow(ll x , ll p)
	{
		ll res = 1;
		x %= MOD , p %= MOD - 1;
		if(p < 0)p += MOD - 1;
		for(; p ; p >>= 1 , x = x * x % MOD) 
			if(p & 1)res = res * x % MOD;
		return res;
	}
	void Init(int n)
	{
		ip2[0] = fac[0] = ifac[0] = 1;
		for(int i = 1 ; i <= n ; i++)
			fac[i] = (ll)fac[i - 1] * i % MOD;
		ifac[n] = Qpow(fac[n] , MOD - 2);
		for(int i = n - 1 ; i >= 1 ; i--)
			ifac[i] = (ll)ifac[i + 1] * (i + 1) % MOD;
		for(int i = 1 ; i <= n ; i++)
			ip2[i] = (ll)ip2[i - 1] * INV2 % MOD;
	}
	int C(int n , int m)
	{
		if(n < m || m < 0)return 0;
		return (ll)fac[n] * ifac[m] % MOD * ifac[n - m] % MOD;
	}
	int F(int k , int m)
	{
		int ans = 0 , s = min(m / 2 , k) , dn = 1;
		for(int i = 0 ; i < m - s ; i++)dn = (ll)dn * (k - i) % MOD;
		for(int j = s ; j >= max(m - k , 0) ; j--)
		{
			ans = (ans + ((ll)dn * ip2[j] % MOD) * ((ll)ifac[j] * ifac[m - j * 2] % MOD)) % MOD;
			dn = (ll)dn * (k - m + j) % MOD;
		}
		ans = (ll)ans * fac[m] % MOD;
		return ans;
	}
	int Solve()
	{
		Init(m);
		int sum = 0 , ans = 0; 
		dk[0] = 1;
		for(int i = 1 ; i <= min(m / 2 , k) ; i++)
			dk[i] = (ll)dk[i - 1] * (k - i + 1) % MOD;
		ipw[0] = 1 , ipw[1] = Qpow(k , MOD - 2);
		for(int i = 2 ; i <= m ; i++)ipw[i] = (ll)ipw[i - 1] * ipw[1] % MOD;
		for(int len = min(m / 2 , k) ; len >= 0 ; len--)
		{
			int c = (ll)dk[len] * F(k - len , m - len * 2) % MOD;
			// cerr << "border len = " << len << " : " << c << "\n";
			if(!len)c = (c + MOD - sum) % MOD;
			else sum = (sum + c) % MOD;
			int res = 0;
			if(!len)res = ll(n - m + 1) * pw[n - m] % MOD;
			else
			{
				int kni = Qpow(k , n - m) , dt = Qpow(Qpow(k , (m - len)) , MOD - 2);
				for(int i = m , l = 2 ; i <= n ; i += m - len , l++ , kni = (ll)kni * dt % MOD) 
				{
					int t = 0 , tt = 0 , r = m - len;
					int p = ll(pw[r] - 1) * ipw[r] % MOD;
					// for(int j =  1 ; j <= n - i + 1 ; j++)
					// {
					// 	auto W = [&](int l)
					// 	{
					// 		if(l >= r)return (ll)p * pw[l] % MOD;
					// 		return (ll)pw[l];
					// 		// return (ll)(pw[min(r , l)] - (l >= r)) * pw[max(l - r , 0)] % MOD;
					// 	};
					// 	tt = (tt + (ll)W(j - 1) * W(n - i - j + 1)) % MOD;
					// }
					if(n - i + 1 >= 2 * r)
					{
						int p2 = (ll)p * p % MOD;
						t = (ll(n - i + 1 - r * 2) * p2 % MOD + ll(2 * r) * p % MOD) % MOD;
					}
					else
					{
						int L = max(0 , n - i - r + 1);
						int R = min(n - i + 2 , r + 1);
						t = (ll(R - L - 1) + ll(L + n - i + 2 - R) * p) % MOD;
						// cerr << "\t" << t << "\n";
					}
					t = (ll)t * kni % MOD;
					// cerr << t << ' ' << tt << " ; " << kni << " = " << Qpow(k , n - i) << " ; " << r << ';' << n - i + 1 << "\n";
					// if(i < n)
					// {
					// 	if(i < n - 1)t = (t + (n - i - 1) * ((ll)(k - 1) * (k - 1) % MOD) % MOD * pw[n - i - 2]) % MOD;
					// 	t = (t + 2ll * (k - 1) % MOD * pw[n - i - 1]) % MOD;
					// }
					// else t = 1;
					res = (res + (ll)(l >> 1) * t) % MOD; 
				}
			}
			// cerr << len << ":" << c << " * " << res << "\n";
			ans = (ans + (ll)res * c) % MOD;
		}
		cout << ans << "\n";
		return 0;
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0) , cout.tie(0);
	cin >> n >> m >> k;
	pw[0] = 1;
	for(int i = 1 ; i <= n ; i++)
		pw[i] = (ll)k * pw[i - 1] % MOD;
	// if(n <= 5 && m <= 5 && k <= 3)
	// BF::Solve();
	NM::Solve();
	return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5740kb

input:

4 2 3

output:

228

result:

ok 1 number(s): "228"

Test #2:

score: 0
Accepted
time: 15ms
memory: 8612kb

input:

999999 1999 12345678

output:

52352722

result:

ok 1 number(s): "52352722"

Test #3:

score: 0
Accepted
time: 0ms
memory: 5740kb

input:

7 4 2

output:

182

result:

ok 1 number(s): "182"

Test #4:

score: 0
Accepted
time: 0ms
memory: 5788kb

input:

4 3 4

output:

480

result:

ok 1 number(s): "480"

Test #5:

score: 0
Accepted
time: 1ms
memory: 5668kb

input:

3 1 1

output:

3

result:

ok 1 number(s): "3"

Test #6:

score: 0
Accepted
time: 1ms
memory: 5716kb

input:

5 5 1

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 0ms
memory: 5744kb

input:

7 4 3

output:

5784

result:

ok 1 number(s): "5784"

Test #8:

score: 0
Accepted
time: 0ms
memory: 5736kb

input:

5 2 4

output:

3932

result:

ok 1 number(s): "3932"

Test #9:

score: 0
Accepted
time: 1ms
memory: 5788kb

input:

8 2 2

output:

1522

result:

ok 1 number(s): "1522"

Test #10:

score: 0
Accepted
time: 0ms
memory: 5788kb

input:

8 1 2

output:

2048

result:

ok 1 number(s): "2048"

Test #11:

score: 0
Accepted
time: 1ms
memory: 5672kb

input:

7 5 3

output:

2430

result:

ok 1 number(s): "2430"

Test #12:

score: 0
Accepted
time: 0ms
memory: 5672kb

input:

10 4 3

output:

272004

result:

ok 1 number(s): "272004"

Test #13:

score: 0
Accepted
time: 4ms
memory: 7840kb

input:

675978 614 2

output:

0

result:

ok 1 number(s): "0"

Test #14:

score: 0
Accepted
time: 2ms
memory: 5796kb

input:

244613 38 1

output:

0

result:

ok 1 number(s): "0"

Test #15:

score: 0
Accepted
time: 1ms
memory: 5744kb

input:

186293 1462 1

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 1ms
memory: 5660kb

input:

24867 886 1

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 5ms
memory: 7804kb

input:

976164 1014 2

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 3ms
memory: 7784kb

input:

179356 2 716844809

output:

577866092

result:

ok 1 number(s): "577866092"

Test #19:

score: 0
Accepted
time: 6ms
memory: 7696kb

input:

621001 130 310625363

output:

892869197

result:

ok 1 number(s): "892869197"

Test #20:

score: 0
Accepted
time: 8ms
memory: 7776kb

input:

678862 850 754662812

output:

582264789

result:

ok 1 number(s): "582264789"

Test #21:

score: 0
Accepted
time: 8ms
memory: 7732kb

input:

650845 978 348443366

output:

825425732

result:

ok 1 number(s): "825425732"

Test #22:

score: 0
Accepted
time: 7ms
memory: 7840kb

input:

669914 402 87448112

output:

318098088

result:

ok 1 number(s): "318098088"

Test #23:

score: 0
Accepted
time: 7ms
memory: 9232kb

input:

998593 530 681228665

output:

408255654

result:

ok 1 number(s): "408255654"

Test #24:

score: 0
Accepted
time: 9ms
memory: 5812kb

input:

369361 1954 125266115

output:

509912384

result:

ok 1 number(s): "509912384"

Test #25:

score: 0
Accepted
time: 12ms
memory: 9268kb

input:

900226 1378 424079373

output:

406320917

result:

ok 1 number(s): "406320917"

Test #26:

score: 0
Accepted
time: 3ms
memory: 5748kb

input:

334887 1506 17859926

output:

503264679

result:

ok 1 number(s): "503264679"

Test #27:

score: 0
Accepted
time: 10ms
memory: 8868kb

input:

936048 544 53978328

output:

548647866

result:

ok 1 number(s): "548647866"

Test #28:

score: 0
Accepted
time: 4ms
memory: 5732kb

input:

152789 1264 792983073

output:

839541707

result:

ok 1 number(s): "839541707"

Test #29:

score: 0
Accepted
time: 7ms
memory: 7856kb

input:

714493 1392 91796331

output:

721071046

result:

ok 1 number(s): "721071046"

Test #30:

score: 0
Accepted
time: 4ms
memory: 7832kb

input:

269571 816 830801077

output:

330064211

result:

ok 1 number(s): "330064211"

Test #31:

score: 0
Accepted
time: 10ms
memory: 9020kb

input:

845120 944 424581630

output:

348960190

result:

ok 1 number(s): "348960190"

Test #32:

score: 0
Accepted
time: 6ms
memory: 7788kb

input:

533990 368 163586376

output:

522092095

result:

ok 1 number(s): "522092095"

Test #33:

score: 0
Accepted
time: 7ms
memory: 5808kb

input:

181707 1792 462399634

output:

373795106

result:

ok 1 number(s): "373795106"

Test #34:

score: 0
Accepted
time: 9ms
memory: 7800kb

input:

417349 1920 761212891

output:

587051329

result:

ok 1 number(s): "587051329"

Test #35:

score: 0
Accepted
time: 8ms
memory: 7736kb

input:

526583 1344 500217637

output:

108767800

result:

ok 1 number(s): "108767800"

Test #36:

score: 0
Accepted
time: 9ms
memory: 7812kb

input:

867054 769 93998191

output:

239123369

result:

ok 1 number(s): "239123369"

Extra Test:

score: 0
Extra Test Passed