博客
关于我
[SDOI2017]序列计数
阅读量:351 次
发布时间:2019-03-04

本文共 1343 字,大约阅读时间需要 4 分钟。

题目

思路

感谢提供了思路。和代码。

基本思路,是从 d p \tt{dp} dp 中脱胎换骨:

如果用 f ( i , j ) f(i,j) f(i,j) 表示 i i i 个数字、余数为 j j j 的答案,那么对于任意 L L L 均有 f ( i , j ) = ∑ x = 0 p − 1 f ( L , x ) × f ( i − L , ( j + p − x )   m o d   p ) f(i,j)=\sum_{x=0}^{p-1}f(L,x)\times f(i-L,(j+p-x)\bmod p) f(i,j)=x=0p1f(L,x)×f(iL,(j+px)modp)

也就是枚举前 L L L 个数字的余数,与剩下的余数相加。

然后我们写一个函数 g ( L , x ) = ∑ i = 0 p − 1 f ( L , i ) x i g(L,x)=\sum_{i=0}^{p-1}f(L,i)x^i g(L,x)=i=0p1f(L,i)xi ,因为 d p \tt{dp} dp 转移非常像卷积。

于是我们就可以写成快速幂的形式。求的答案就是 g n ( 1 , x ) g^n(1,x) gn(1,x) 中的某一项。

嗯?你问我怎么处理 “有一个质数” ?正难则反,减去全部是合数的情况即可。

代码

#include
#include
#define mod 20170408using namespace std;int n,m,p,vis[20000010],cnt,pre[2000000];long long f[210],F[210],g[210],G[210];long long c[210],x=1;void work(long long *a,long long *b,long long *d) { int i,j; for(i=0; i
>n>>m>>p; F[0]=G[0]=f[1]=g[1]=1; for(i=2; i<=m; i++) { f[i%p]++; //此时的f为f[1][0--p]的值 if(!vis[i])pre[++cnt]=i; else g[i%p]++; //通过欧拉筛记录g[1][0--p]的值 for(j=1; pre[j]*i<=m; j++) { vis[pre[j]*i]=1; if(!(i%pre[j])) break; } } while(n) { if(n&1) work(F,f,F),work(G,g,G); work(f,f,f); work(g,g,g); n>>=1; } cout<<(F[0]-G[0]+mod)%mod; //特别注意这里,学长特别提醒我了的。F[0]可能比G[0]小。 return 0;}

转载地址:http://lovq.baihongyu.com/

你可能感兴趣的文章
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>