码迷,mamicode.com
首页 > 其他好文 > 详细

P3702 [SDOI2017]序列计数

时间:2019-02-13 22:55:58      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:long   int   std   代码   char   return   targe   span   const   

P3702 [SDOI2017]序列计数

链接

分析:

 

代码: 

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;

inline int read() {
    int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch==-)f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-0;return x*f;
}

const int N = 20000005, mod = 20170408;
int pri[N], n, m, p, tot;
bool nopri[N];

struct Poly{
    int a[105];
    Poly() { memset(a, 0, sizeof(a)); }
}A, B;
Poly operator * (const Poly &A, const Poly &B) {
    Poly C;
    for (int i = 0; i < p; ++i) 
        for (int j = 0; j < p; ++j) 
            (C.a[(i + j) % p] += (1ll * A.a[i] * B.a[j]) % mod) %= mod;
    return C;
}
void init(int n) {
    nopri[1] = true;
    for (int i = 2; i <= n; ++i) {
        if (!nopri[i]) pri[++tot] = i;
        for (int j = 1; j <= tot && pri[j] * i <= n; ++j) {
            nopri[i * pri[j]] = true;
            if (i % pri[j] == 0) break;
        }
    }
}
Poly ksm(Poly A,int b) {
    Poly res = A; b --;
    while (b) {
        if (b & 1) res = res * A;
        A = A * A;
        b >>= 1;
    }
    return res;
}
int main() {
    n = read(), m = read(); p = read();
    init(m);
    for (int i = 1; i <= m; ++i) {
        A.a[i % p] ++;
        if (nopri[i]) B.a[i % p] ++;
    }
    A = ksm(A, n); B = ksm(B, n);
    cout << (A.a[0] - B.a[0] + mod) % mod;
    return 0;
}

 

P3702 [SDOI2017]序列计数

标签:long   int   std   代码   char   return   targe   span   const   

原文地址:https://www.cnblogs.com/mjtcn/p/10372077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!