QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#418626 | #8717. 骰子 | tzxydby# | TL | 1ms | 4392kb | C++14 | 4.7kb | 2024-05-23 14:52:20 | 2024-05-23 14:52:21 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#define mo 1000000007
using namespace std;
int n,m,q,in1,in2,b[210],p[1510][210],res[210];
namespace mtt
{
const int N=1000,mod=1e9+7;
const double pi=acos(-1);
struct com
{
double r,i;
com(){}
com(double _r,double _i):r(_r),i(_i){}
com operator+(const com x){return {r+x.r,i+x.i};}
com operator-(const com x){return {r-x.r,i-x.i};}
com operator*(const com x){return {r*x.r-i*x.i,r*x.i+i*x.r};}
com operator/(const double x){return {r/x,i/x};}
com conj(){return {r,-i};}
};
int m,r[N],l;
int a[N],b[N],x[N],y[N];
com w[N];
void fft(com *a)
{
for(int i=0;i<m;i++)
if(i<r[i])
swap(a[i],a[r[i]]);
for(int i=1;i<m;i<<=1)
{
for(int j=0;j<m;j+=(i<<1))
{
for(int k=0;k<i;k++)
{
com x=a[j+k],y=w[m/(i<<1)*k]*a[j+k+i];
a[j+k]=x+y;
a[j+k+i]=x-y;
}
}
}
}
void cal(int *x,int *y)
{
com a[N],b[N],da[N],db[N],dc[N],dd[N];
for(int i=0;i<m;i++)
{
a[i]=com(x[i]&32767,x[i]>>15);
b[i]=com(y[i]&32767,y[i]>>15);
}
fft(a),fft(b);
for(int i=0;i<m;i++)
{
int j=(m-i)&(m-1);
com va,vb,vc,vd;
va=(a[i]+a[j].conj())*com(0.5,0);
vb=(a[i]-a[j].conj())*com(0,-0.5);
vc=(b[i]+b[j].conj())*com(0.5,0);
vd=(b[i]-b[j].conj())*com(0,-0.5);
da[j]=va*vc;
db[j]=va*vd;
dc[j]=vb*vc;
dd[j]=vb*vd;
}
for(int i=0;i<m;i++)
{
a[i]=da[i]+db[i]*com(0,1);
b[i]=dc[i]+dd[i]*com(0,1);
}
fft(a),fft(b);
for(int i=0;i<m;i++)
{
long long va=a[i].r/m+0.5,vb=a[i].i/m+0.5,vc=b[i].r/m+0.5,vd=b[i].i/m+0.5;
va%=mod,vb%=mod,vc%=mod,vd%=mod;
x[i]=(va+((vb+vc)<<15)+((vd)<<30))%mod;
}
}
vector<int>mul(vector<int>a,vector<int>b)
{
m=1,l=0;
int ca=a.size(),cb=b.size();
while(m<ca+cb-1)
m<<=1,l++;
for(int i=0;i<m;i++)
r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
for(int i=0;i<m;i++)
w[i]=com(cos(2*pi*i/m),sin(2*pi*i/m));
for(int i=0;i<m;i++)
x[i]=y[i]=0;
for(int i=0;i<ca;i++)
x[i]=a[i];
for(int i=0;i<cb;i++)
y[i]=b[i];
cal(x,y);
a.resize(ca+cb-1);
for(int i=0;i<a.size();i++)
a[i]=x[i];
return a;
}
}
inline int MO(int x)
{
return x<mo?x:x-mo;
}
inline void wk(int* a,int* b,int* c)
{
vector<int>bb,cc;
for(int i=0;i<=m;i++)
{
bb.push_back(b[i]);
}
for(int i=0;i<=m;i++)
{
cc.push_back(c[i]);
}
vector<int>aa=mtt::mul(bb,cc);
for(int i=0;i<=m;i++)
{
a[i]=aa[i];
}
}
struct node
{
int **dat;
node *lch,*rch;
node(int l,int r)
{
if(l==r)
{
dat=nullptr;
return;
}
dat=new int*[r-l+1];
for(int i=0;i<=r-l;i++)
{
dat[i]=new int[m+1];
}
int md=l+r>>1;
memcpy(dat[md-l],p[md],sizeof(int)*(m+1));
memcpy(dat[md+1-l],p[md+1],sizeof(int)*(m+1));
for(int i=md-1;i>=l;i--)
{
wk(dat[i-l],dat[i-l+1],p[i]);
}
for(int i=md+2;i<=r;i++)
{
wk(dat[i-l],dat[i-l-1],p[i]);
}
lch=new node(l,md);
rch=new node(md+1,r);
}
inline int wkot(int x,int y,int l,int r)
{
int md=l+r>>1;
if(x==md+1)
{
int ans=0;
for(int i=0;i<=m;i++)
{
ans=MO(ans+1ll*b[i]*dat[y-l][i]%mo);
}
return ans;
}
else if(y==md)
{
int ans=0;
for(int i=0;i<=m;i++)
{
ans=MO(ans+1ll*b[i]*dat[x-l][i]%mo);
}
return ans;
}
else if(x<=md&&y>md)
{
int ans=0;
wk(res,dat[x-l],dat[y-l]);
for(int i=0;i<=m;i++)
{
ans=MO(ans+1ll*b[i]*res[i]%mo);
}
return ans;
}
else return y<md?lch->wkot(x,y,l,md):rch->wkot(x,y,md+1,r);
}
};
node *no;
int main()
{
// freopen("1.in","r",stdin);
scanf("%d%d%d",&n,&m,&q);
for(int i=0;i<=m;i++)
{
scanf("%d",&b[i]);
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
scanf("%d",&p[i][j]);
p[i][j]=MO(p[i][j]);
}
}
no=new node(1,n);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&in1,&in2);
if(n==1)
{
int ans=0;
for(int i=0;i<=m;i++)
{
ans=MO(ans+1ll*p[1][i]*b[i]%mo);
}
printf("%d\n",ans);
}
else printf("%d\n",no->wkot(in1,in2,1,n));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4392kb
input:
3 3 3 4 3 2 1 0 1 0 1000000007 0 500000004 0 500000004 0 0 500000004 500000004 1 1 1 2 1 3
output:
3 1 0
result:
ok 3 number(s): "3 1 0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 4096kb
input:
3 3 6 4 3 2 1 1000000007 0 1 0 1000000007 1 0 0 1000000007 0 1 0 1 1 1 2 1 3 2 2 2 3 3 3
output:
2 1 0 3 1 2
result:
ok 6 numbers
Test #3:
score: 0
Accepted
time: 1ms
memory: 3904kb
input:
1 1 1 604063100 57375033 742299910 257700098 1 1
output:
148903503
result:
ok 1 number(s): "148903503"
Test #4:
score: -100
Time Limit Exceeded
input:
1500 200 600000 253665324 876103781 804024983 929290295 908790466 176299158 528078340 696679927 416465140 509641654 705083449 361711737 250659645 735832780 35321360 383752049 203979021 178832532 785212637 514502839 169840231 65809146 504755349 516829442 382478309 901925498 142312128 782336477 741339...
output:
66394849 858043015 290088512 433850735 16359498 544692508 495705795 390858705 334940115 441003348 589429674 891250455 147055038 949270774 782296292 854444995 608076278 772991067 609961969 3444634 534397763 659524291 384815421 329963211 259265811 214554716 662015873 465616975 355211926 398786302 7484...