QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#722270 | #9612. 月亮的背面是粉红色的 | Larunatrecy | 100 ✓ | 1592ms | 495596kb | C++14 | 9.7kb | 2024-11-07 18:24:21 | 2024-11-07 18:24:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using u32 = int32_t;
using i64 = int64_t;
using u64 = uint64_t;
using i128 = __int128_t;
using ci = const int;
typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull red(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
}P(1);
const int mod = 1e9+7;
inline int mul(int x,int y){return P.red(1ll*x*y);}
inline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline int sub(int x,int y){return x-y<0?x-y+mod:x-y;}
typedef long long LL;
struct Fact {
i64 x, y;
Fact() : x(0), y(0) {}
Fact(i64 _x, i64 _y) : x(_x), y(_y) {}
inline Fact operator+(const Fact &o) const {
return Fact(x + o.x, y + o.y);
}
};
const int N = 1e7+7;
int vi[N],prime[N],tot=0,pc[N],F[N],s[N],mu[N];
int m,K;
struct info
{
int w[2];
}S[N],Mu[N];
void init(int n)
{
s[1]=1;mu[1]=1;F[1]=1;
for(int i=2;i<=n;i++)
{
if(!vi[i])
{
vi[i]=i;
prime[++tot]=i;
mu[i]=mod-1;
F[i]=2;s[i]=2;pc[i]=2;
}
for(int j=1;j<=tot;j++)
{
if(prime[j]>vi[i]||1ll*i*prime[j]>n)break;
int k=i*prime[j];
vi[k]=prime[j];
if(i%prime[j]==0)
{
pc[k]=pc[i]+1;
s[k]=s[i]/pc[i]*pc[k];
pc[k]=pc[i]+1;
F[k]=F[i];
break;
}
s[k]=s[i]*2;
pc[k]=2;
mu[k]=mod-mu[i];
F[k]=F[i]*2;
}
}
for(int i=1;i<=n;i++)
{
S[i].w[0]=add(S[i-1].w[0],s[i]);
Mu[i].w[0]=add(Mu[i-1].w[0],mu[i]);
S[i].w[1]=add(S[i-1].w[1],mul(s[i],i));
Mu[i].w[1]=add(Mu[i-1].w[1],1ll*mu[i]*mul(i,i)%mod);
}
}
typedef long long LL;
struct val
{
int w[3][3];
void clr()
{
w[1][0]=w[0][1]=w[1][1]=w[2][0]=w[0][2]=0;
}
};
int G[20][20],B[20],C[20][20],inv[20];
inline int sign(int x){return (x&1)?mod-1:1;}
void prework()
{
int n=10;
inv[1]=1;
for(int i=2;i<=n;i++)
inv[i]=1ll*inv[mod%i]*(mod-mod/i)%mod;
B[0]=1;
C[0][0]=1;
for(int i=1;i<=n;i++)
{
C[i][0]=1;
for(int j=1;j<=i;j++)
C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
}
for(int i=1;i<=n;i++)
for(int j=0;j<=i-1;j++)
B[i]=(B[i]-1ll*C[i][j]*B[j]%mod*inv[i-j+1]%mod+mod)%mod;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
G[i][i+1-j]=1ll*inv[i+1]*sign(j)%mod*C[i+1][j]%mod*B[j]%mod;
}
}
inline int Sum0(int n){return n;}
inline int Sum1(int n){return P.red(1ll*P.red(1ll*n*(n+1))*inv[2]);}
inline int Sum2(int n){return P.red(1ll*P.red(1ll*n*(n+1))*P.red(1ll*(2*n+1)*inv[6]));}
inline int Sum3(int n){int v=P.red(1ll*P.red(1ll*n*(n+1))*inv[2]);return P.red(1ll*v*v);}
inline int Sumk(int n,int k)
{
if(k==0)return n;
if(k==1)return P.red(1ll*P.red(1ll*n*(n+1))*inv[2]);
if(k==2)return P.red(1ll*P.red(1ll*n*(n+1))*P.red(1ll*(2*n+1)*inv[6]));
int v=P.red(1ll*P.red(1ll*n*(n+1))*inv[2]);
return P.red(1ll*v*v);
}
int tmpa[10],tmpb[10];
int R,tmp[10];
typedef unsigned long long ULL;
int test=0;
i128 Val[3][3];
val mapa[3000000];
void solo(int x)
{
mapa[x].w[1][1]=Val[1][1]%mod;Val[1][1]=0;
mapa[x].w[0][1]=Val[0][1]%mod;Val[0][1]=0;
mapa[x].w[1][0]=Val[1][0]%mod;Val[1][0]=0;
mapa[x].w[2][0]=Val[2][0]%mod;Val[2][0]=0;
mapa[x].w[0][2]=Val[0][2]%mod;Val[0][2]=0;
}
int Test=0;
unordered_map<ULL,int> id;
int idx=0;
void euclid(LL A,LL B)
{
while(B)
{
if(B>=A)
{
B%=A;
}
else swap(A,B);
}
}
namespace H
{
struct edge
{
LL A,B;
int next,id;
}e[3000200];
const int M = 2e7+3;
int link[M],t=0;
void ins(LL A,LL B,int id)
{
int x=(A%M*13331%M+B*131%M)%M;
e[++t].A=A;
e[t].B=B;
e[t].id=id;
e[t].next=link[x];
link[x]=t;
}
int qry(LL A,LL B)
{
int x=(A%M*13331%M+B*131%M)%M;
for(int i=link[x];i;i=e[i].next)
if(e[i].A==A&&e[i].B==B)return e[i].id;
return -1;
}
}
int Euclid(LL A,LL B)
{
if(!B)return 0;
int d=H::qry(A,B);
if(d!=-1)return d;
int x=++idx;H::ins(A,B,x);
++Test;
if(B>=A)
{
int y=Euclid(A,B%A);
int c=(B/A)%mod,u=(A%mod);
tmp[1]=Sum1(u);tmp[2]=Sum2(u);tmp[3]=Sum3(u);
for(int p=0;p<=R;p++)
{
if(p>0)
{
Val[p][0]+=mapa[y].w[p][0];
Val[p][0]+=1ll*c*tmp[p+1];
}
if(R-p>=1)
{
Val[p][1]+=mapa[y].w[p][1];
Val[p][1]+=(i128)c*mapa[y].w[p+1][0];
Val[p][1]+=(i128)G[1][1]*c*tmp[p+1];
Val[p][1]+=(i128)G[1][2]*c*c%mod*tmp[p+2];
}
if(R-p>=2)
{
Val[p][2]+=mapa[y].w[p][2];
Val[p][2]+=(i128)c*mapa[y].w[1][1]*2;
Val[p][2]+=(i128)c*c*mapa[y].w[2][0];
Val[p][2]+=(i128)G[2][1]*c*tmp[1];
Val[p][2]+=(i128)G[2][2]*c*c%mod*tmp[2];
Val[p][2]+=(i128)G[2][3]*c*c*c%mod*tmp[3];
}
}
solo(x);
return x;
}
int y=Euclid(B,A);
int u=(A%mod),v=(B%mod);
tmpa[0]=tmpb[0]=1;
tmpa[0]=Sum0(u);tmpb[0]=Sum0(v);
tmpa[1]=Sum1(u);tmpb[1]=Sum1(v);
tmpa[2]=Sum2(u);tmpb[2]=Sum2(v);
Val[1][0]=1ll*tmpa[1]*tmpb[0]-mapa[y].w[0][1]+u;
Val[0][1]=1ll*tmpa[0]*tmpb[1]-mapa[y].w[1][0]+v;
Val[1][1]=1ll*tmpa[1]*tmpb[1]-mapa[y].w[1][1]+1ll*u*v;
Val[2][0]=1ll*tmpa[2]*tmpb[0]-mapa[y].w[0][2]+1ll*u*u;
Val[0][2]=1ll*tmpa[0]*tmpb[2]-mapa[y].w[2][0]+1ll*v*v;
solo(x);
return x;
}
i128 Ret[5];
info ask(LL n)
{
Ret[0]=Ret[1]=0;
if(n<=K)return S[n];
const int Q = cbrt(n);
LL B=sqrt(n),x=n/B,y=B+1;
static vector<Fact> q;
q.push_back(Fact(1, 0));
q.push_back(Fact(1, 1));
auto ck=[&n](LL x,LL y) {return n<x*y;};
auto judge=[&n](LL x,Fact v) {return i128(n)*v.x<=i128(x)*x*v.y;};
while(1)
{
Fact L=q.back();q.pop_back();
Fact R=q.back(),M;
int u=Euclid(L.x,L.y);
while(ck(x+L.x,y-L.y))
{
Ret[0] += x * L.y + (L.y + 1) * (L.x - 1) / 2;
Ret[1]+=(i128)(Sum1((x-1)%mod))*(Sum1(y-1)-Sum1(y-L.y-1));
LL x0=x+L.x,y0=y-L.y;
Ret[1]+=(i128)x0*mapa[u].w[0][1];
Ret[1]-=(i128)y0*(mapa[u].w[1][0]+Sum1(P.red(L.x)));
Ret[1]-=mapa[u].w[1][1];
Ret[1]+=(i128)x0*y0*((L.y + 1)*(L.x+1)/2+1);
Ret[1]-=(i128)x*y;
Ret[1]-=(i128)(x+L.x)*(y-L.y);
x+=L.x,y-=L.y;
}
if (y <= Q) break;
while (!ck(x + R.x, y - R.y)) {
L = R;
q.pop_back();
R = q.back();
}
while (1) {
M = L + R;
if (ck(x + M.x, y - M.y)) q.push_back(R = M);
else {
if (judge(x + M.x, R)) break;
L = M;
}
}
}
for (int i = 1; i < y; ++i)
{
Ret[0]+=(n/i);
Ret[1]+=1ll*i*Sum1((n/i)%mod);
}
Ret[0]=Ret[0]*2-1ll*B*B;
Ret[1]=Ret[1]*2-1ll*Sum1(B)*Sum1(B);
Ret[0]=(Ret[0]%mod+mod)%mod;
Ret[1]=(Ret[1]%mod+mod)%mod;
info ret;ret.w[0]=Ret[0];ret.w[1]=Ret[1];
return ret;
}
unordered_map<LL,info> vis;
info Sum(LL n)
{
if(n<=K)return Mu[n];
if(vis.find(n)!=vis.end())return vis[n];
i128 w0=1,w1=1;
LL l=2,r=n;
for(;l<=n;l=r+1)
{
r=(n/(n/l));
info W=Sum(n/l);
w0-=1ll*(Sum0(r%mod)-Sum0((l-1)%mod))*W.w[0];
w1-=1ll*(Sum2(r%mod)-Sum2((l-1)%mod))*W.w[1];
}
info ret=(info){(w0%mod+mod)%mod,(w1%mod+mod)%mod};
return vis[n]=ret;
}
LL ask0(LL n)
{
i128 res=0;
if(n<=K)return S[n].w[0];
const int Q = cbrt(n);
LL B=sqrt(n),x=n/B,y=B+1;
static vector<Fact> q;
q.push_back(Fact(1, 0));
q.push_back(Fact(1, 1));
auto ck=[&n](LL x,LL y) {return n<x*y;};
auto judge=[&n](LL x,Fact v) {return i128(n)*v.x<=i128(x)*x*v.y;};
while(1)
{
Fact L=q.back();q.pop_back();
Fact R=q.back(),M;
while(ck(x+L.x,y-L.y))
{
res+=x * L.y + (L.y + 1) * (L.x - 1) / 2;
x+=L.x,y-=L.y;
}
if (y <= Q) break;
while (!ck(x + R.x, y - R.y)) {
L = R;
q.pop_back();
R = q.back();
}
while (1) {
M = L + R;
if (ck(x + M.x, y - M.y)) q.push_back(R = M);
else {
if (judge(x + M.x, R)) break;
L = M;
}
}
}
for (int i = 1; i < y; ++i)res+=(n/i);
res%=mod;res=res*2-B*B%mod;res=(res%mod+mod)%mod;
return res%mod;
}
LL Ans[10];
int main()
{
//freopen("Sum.in","r",stdin);
//freopen("Sum.out","w",stdout);
LL n,tp;
cin>>n>>tp;
if(n>=1e13)K=1e7;
else K=1e6;
prework();
m=1;P=FastMod(mod);init(K);R=2;
if(tp==0)
{
LL l=1,r;
for(;l*l<=n;l=r+1)
{
r=sqrt(n/(n/(l*l)));
if(l==r&&r<=K&&mu[l]==0);
else
{
info R=Sum(r),L=Sum(l-1);
LL W=ask0(n/(l*l));
Ans[0]+=1ll*(R.w[0]-L.w[0])*W%mod;
}
}
Ans[0]%=mod;
Ans[0]=(Ans[0]%mod+mod)%mod;
cout<<1ll*Ans[0]*Ans[0]%mod;
return 0;
}
LL l=1,r;
for(;l*l<=n;l=r+1)
{
r=sqrt(n/(n/(l*l)));
if(l==r&&r<=K&&mu[l]==0);
else
{
info R=Sum(r),L=Sum(l-1),W=ask(n/(l*l));
Ans[0]+=1ll*(R.w[0]-L.w[0])*W.w[0]%mod;
Ans[1]+=1ll*(R.w[1]-L.w[1])*W.w[1]%mod;
}
}
Ans[0]%=mod;Ans[1]%=mod;
cout<<1ll*Ans[0]*Ans[0]%mod<<' '<<1ll*Ans[1]*Ans[1]%mod<<endl;
return 0;
}
详细
Subtask #1:
score: 3
Accepted
Test #1:
score: 3
Accepted
time: 19ms
memory: 54648kb
input:
1726 1
output:
84290761 74619067
result:
ok 2 number(s): "84290761 74619067"
Test #2:
score: 3
Accepted
time: 21ms
memory: 52932kb
input:
3608 1
output:
433014481 672891299
result:
ok 2 number(s): "433014481 672891299"
Test #3:
score: 3
Accepted
time: 16ms
memory: 54468kb
input:
2921 1
output:
271096225 547734266
result:
ok 2 number(s): "271096225 547734266"
Subtask #2:
score: 3
Accepted
Test #4:
score: 3
Accepted
time: 15ms
memory: 58024kb
input:
6116899 1
output:
219318963 301450440
result:
ok 2 number(s): "219318963 301450440"
Test #5:
score: 3
Accepted
time: 17ms
memory: 56228kb
input:
6260707 1
output:
148720176 263856753
result:
ok 2 number(s): "148720176 263856753"
Test #6:
score: 3
Accepted
time: 11ms
memory: 59308kb
input:
6763677 1
output:
944542490 136397156
result:
ok 2 number(s): "944542490 136397156"
Subtask #3:
score: 8
Accepted
Test #7:
score: 8
Accepted
time: 19ms
memory: 53828kb
input:
6469467712 0
output:
147393348
result:
ok 1 number(s): "147393348"
Test #8:
score: 8
Accepted
time: 17ms
memory: 52796kb
input:
8967004453 0
output:
229436583
result:
ok 1 number(s): "229436583"
Test #9:
score: 8
Accepted
time: 11ms
memory: 50656kb
input:
6636594384 0
output:
995965072
result:
ok 1 number(s): "995965072"
Subtask #4:
score: 8
Accepted
Test #10:
score: 8
Accepted
time: 11ms
memory: 70732kb
input:
8292948816 1
output:
566765721 287485757
result:
ok 2 number(s): "566765721 287485757"
Test #11:
score: 8
Accepted
time: 25ms
memory: 69604kb
input:
8592748771 1
output:
649470692 164561252
result:
ok 2 number(s): "649470692 164561252"
Test #12:
score: 8
Accepted
time: 23ms
memory: 69204kb
input:
9827380808 1
output:
291159931 188690805
result:
ok 2 number(s): "291159931 188690805"
Subtask #5:
score: 8
Accepted
Test #13:
score: 8
Accepted
time: 75ms
memory: 109804kb
input:
900472451132 1
output:
247050500 963765719
result:
ok 2 number(s): "247050500 963765719"
Test #14:
score: 8
Accepted
time: 58ms
memory: 110944kb
input:
850862494659 1
output:
200210720 915108650
result:
ok 2 number(s): "200210720 915108650"
Test #15:
score: 8
Accepted
time: 51ms
memory: 116288kb
input:
851346512859 1
output:
895785763 504512885
result:
ok 2 number(s): "895785763 504512885"
Subtask #6:
score: 10
Accepted
Test #16:
score: 10
Accepted
time: 169ms
memory: 142304kb
input:
9864907300784 1
output:
359943536 720268421
result:
ok 2 number(s): "359943536 720268421"
Test #17:
score: 10
Accepted
time: 144ms
memory: 136224kb
input:
8181674676063 1
output:
839993102 994056029
result:
ok 2 number(s): "839993102 994056029"
Test #18:
score: 10
Accepted
time: 182ms
memory: 143076kb
input:
9893510217522 1
output:
157499971 930653488
result:
ok 2 number(s): "157499971 930653488"
Subtask #7:
score: 13
Accepted
Test #19:
score: 13
Accepted
time: 340ms
memory: 362164kb
input:
96735749745529 0
output:
223354886
result:
ok 1 number(s): "223354886"
Test #20:
score: 13
Accepted
time: 347ms
memory: 361468kb
input:
95243570720799 0
output:
555372474
result:
ok 1 number(s): "555372474"
Test #21:
score: 13
Accepted
time: 341ms
memory: 361904kb
input:
97668723090105 0
output:
84562124
result:
ok 1 number(s): "84562124"
Subtask #8:
score: 14
Accepted
Test #22:
score: 14
Accepted
time: 617ms
memory: 462460kb
input:
94060593399194 1
output:
52991150 887133157
result:
ok 2 number(s): "52991150 887133157"
Test #23:
score: 14
Accepted
time: 666ms
memory: 462084kb
input:
98527940728119 1
output:
281611635 910356955
result:
ok 2 number(s): "281611635 910356955"
Test #24:
score: 14
Accepted
time: 656ms
memory: 461368kb
input:
90501814019947 1
output:
666385143 229785369
result:
ok 2 number(s): "666385143 229785369"
Subtask #9:
score: 16
Accepted
Test #25:
score: 16
Accepted
time: 1153ms
memory: 361832kb
input:
9772457586483846 0
output:
631039552
result:
ok 1 number(s): "631039552"
Test #26:
score: 16
Accepted
time: 1122ms
memory: 361428kb
input:
9889806164705403 0
output:
169322134
result:
ok 1 number(s): "169322134"
Test #27:
score: 16
Accepted
time: 1138ms
memory: 361640kb
input:
9422498258316766 0
output:
413943782
result:
ok 1 number(s): "413943782"
Test #28:
score: 16
Accepted
time: 1124ms
memory: 360256kb
input:
9845978636381962 0
output:
857401052
result:
ok 1 number(s): "857401052"
Test #29:
score: 16
Accepted
time: 1130ms
memory: 360904kb
input:
9761171951453691 0
output:
205712009
result:
ok 1 number(s): "205712009"
Test #30:
score: 16
Accepted
time: 1113ms
memory: 361404kb
input:
9224667465673566 0
output:
143979878
result:
ok 1 number(s): "143979878"
Subtask #10:
score: 17
Accepted
Test #31:
score: 17
Accepted
time: 1553ms
memory: 495152kb
input:
949243849085176 1
output:
508465534 771553755
result:
ok 2 number(s): "508465534 771553755"
Test #32:
score: 17
Accepted
time: 1547ms
memory: 494232kb
input:
924225524519163 1
output:
867410272 870831653
result:
ok 2 number(s): "867410272 870831653"
Test #33:
score: 17
Accepted
time: 1592ms
memory: 495596kb
input:
978079151303393 1
output:
235076358 675828942
result:
ok 2 number(s): "235076358 675828942"
Test #34:
score: 17
Accepted
time: 1579ms
memory: 494176kb
input:
929804617107620 1
output:
790604296 73162158
result:
ok 2 number(s): "790604296 73162158"
Test #35:
score: 17
Accepted
time: 1550ms
memory: 494668kb
input:
989806727450552 1
output:
378550840 783149232
result:
ok 2 number(s): "378550840 783149232"