要求最后乘积为质数,则必须为prim*1*1*...*1
e为正整数(当时以为是自然常数一直卡着做不出来)
如果从1往左右找prim,可行但是复杂,所以从prim往左右找1的个数
最后ans+=(pre[i]+1)*(suf[i]+1)-1
#include<cstdio> #include<cstdlib> #include<vector> #include<cmath> #include<algorithm> #include<cstring> #include<iostream> #include<queue> #define ll long long using namespace std; inline int read() { int x=0;char c=getchar(); while(c<'0' || c>'9' ) c=getchar(); while(c>='0'&&c<='9' ) x=(x<<3)+(x<<1)+c-'0',c=getchar(); return x; } int T,n,e; const int N=2e5+10,M=1e6+10; queue <int > q_0,q_1; int d[N],pre[N],suf[N]; bool not_prim[M]; void get_prim() { not_prim[1]=true; for(int i=2;i<M;i++) for(int j=2;i*j<M;j++) not_prim[i*j]=true; } void prepare() { for(int i=1;i<=n;i++) { if(i-e>0 && d[i-e]==1 ) pre[i]=pre[i-e]+1; else pre[i]=0; } for(int i=n;i>0;i--) { if(i+e<=n && d[i+e]==1 ) suf[i]=suf[i+e]+1; else suf[i]=0; } } int main() { get_prim(); T=read(); while(T--) { n=read(),e=read(); for(int i=1;i<=n;i++) d[i]=read(); prepare(); ll ans=0; for(int i=1;i<=n;i++) { if(!not_prim[d[i]] ) { //printf(" %d %d %d \n",d[i],pre[i],suf[i]); ans+=1LL*(pre[i]+1)*(suf[i]+1)-1; } } printf("%lld\n",ans); } return 0; }View Code