• [SDOI2017]序列计数


    嘟嘟嘟


    这题有那么难????
    提供一个吊打std的做法


    直接令(dp[i][j][0/1])表示前(i)个数的和模(p)(j),且这(i)个数中没有/有质数的方案数。
    先想一下暴力,枚举第(i)个数是哪一个,然后根据这个数是否是质数转移即可。复杂度(O(nmp))


    优化:
    发现(n leqslant 10 ^ 9),就能想到多项式快速幂,转移的时候分四种情况讨论。因为(p)很小,暴力乘就能过,复杂度(O(m + p^ 2 logn))(O(m))是筛质数复杂度)。


    这 题 就 没 了。

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<queue>
    #include<vector>
    #include<ctime>
    #include<assert.h>
    using namespace std;
    #define enter puts("")
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    #define forE(i, x, y) for(int i = head[x], y; (y = e[i].to) && ~i; i = e[i].nxt)
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxs = 105;
    const int maxm = 2e7 + 5;
    const int maxp = 105;
    const ll mod = 20170408; 
    In ll read()
    {
    	ll ans = 0;
    	char ch = getchar(), las = ' ';
    	while(!isdigit(ch)) las = ch, ch = getchar();
    	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    	if(las == '-') ans = -ans;
    	return ans;
    }
    In void write(ll x)
    {
    	if(x < 0) putchar('-'), x = -x;
    	if(x >= 10) write(x / 10);
    	putchar(x % 10 + '0');
    }
    In void MYFILE()
    {
    #ifndef mrclr
    	freopen("ha.in", "r", stdin);
    	freopen("ha.out", "w", stdout);
    #endif
    }
    
    int n, m, p;
    
    In ll inc(ll a, ll b) {return a + b < mod ? a + b : a + b - mod;}
    
    int prm[maxm], v[maxm];
    In void init()
    {
    	for(int i = 2; i < maxm; ++i)
    	{
    		if(!v[i]) v[i] = i, prm[++prm[0]] = i;
    		for(int j = 1; j <= prm[0] && i * prm[j] < maxm; ++j)
    		{
    			v[i * prm[j]] = prm[j];
    			if(i % prm[j] == 0) break;
    		}
    	}
    }
    
    ll dp[maxs][maxp][2];
    In void work0()
    {
    	dp[0][0][0] = 1;
    	for(int i = 1; i <= n; ++i)
    	{
    		for(int j = 0; j < p; ++j)
    			for(int k = 1; k <= m; ++k) 
    			{
    				int t = (j + k) % p;
    				if(v[k] ^ k) dp[i][t][0] = inc(dp[i][t][0], dp[i - 1][j][0]);
    				dp[i][t][1] = inc(dp[i][t][1], dp[i - 1][j][1]);
    				if(v[k] == k) dp[i][t][1] = inc(dp[i][t][1], dp[i - 1][j][0]);
    			}
    	}
    	write(dp[n][0][1]), enter;
    }
    
    ll f[maxp][2], g[maxp][2], c[maxp][2];
    In void mul(ll a[][2], ll b[][2], bool flg)
    {
    	Mem(c, 0);
    	for(int i = 0; i < p; ++i)
    		for(int j = 0; j < p; ++j)
    		{
    			int t = i + j < p ? i + j : i + j - p;
    			c[t][0] = inc(c[t][0], a[i][0] * b[j][0] % mod);
    			c[t][1] = inc(c[t][1], a[i][1] * b[j][0] % mod);
    			c[t][1] = inc(c[t][1], a[i][0] * b[j][1] % mod);
    			c[t][1] = inc(c[t][1], a[i][1] * b[j][1] % mod);
    		}
    	memcpy(a, c, sizeof(c));
    }
    In ll Quickpow(int n)
    {
    	f[0][0] = 1;
    	for(int i = 1; i <= m; ++i) ++g[i % p][v[i] == i];
    	for(; n; n >>= 1, mul(g, g, 0))
    		if(n & 1) mul(f, g, 1);
    	return f[0][1];
    }
    
    int main()
    {
    //	MYFILE();
    	init();
    	n = read(), m = read(), p = read();
    	if(n <= 100) {work0(); return 0;}
    	write(Quickpow(n)), enter;
    	return 0;
    }
    
  • 相关阅读:
    洛谷P1306 斐波那契公约数
    Codevs 1688 求逆序对(权值线段树)
    poj1006 Biorhythms
    2017-9-2 NOIP模拟赛
    洛谷P1633 二进制
    洛谷P2513 [HAOI2009]逆序对数列
    洛谷P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower
    洛谷P2285 [HNOI2004]打鼹鼠
    2017-8-31 NOIP模拟赛
    洛谷P2134 百日旅行
  • 原文地址:https://www.cnblogs.com/mrclr/p/11129099.html
Copyright © 2020-2023  润新知