# [BZOJ4815][CQOI2017]小Q的表格(莫比乌斯反演)

## 4815: [Cqoi2017]小Q的表格

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 832  Solved: 342
[Submit][Status][Discuss]

## Input

1<=m<=10000,1<=a,b,k<=N<=4*10^6,0<=x<=10^18

3 3
1 1 1 2
2 2 4 3
1 2 4 2

9
36
14

1 2 3 2 4 6
2 4 6 4 4 12
3 6 9 6 12 9

## Source

[Submit][Status][Discuss]

https://blog.sengxian.com/solutions/bzoj-4815

 1 #include<cmath>
2 #include<cstdio>
3 #include<algorithm>
4 #define rep(i,l,r) for (int i=l; i<=r; i++)
5 typedef long long ll;
6 using namespace std;
7
8 const int N=4000100,mod=1000000007;
9 bool pr[N];
10 ll x;
11 int n,m,Q,bl,a,b,k,tot,c[N],pos[N],phi[N],p[N],v[N],f[N],g[N];
12 struct B{ int l,r; }B[2010];
13 int gcd(int a,int b){ return (!b) ? a : gcd(b,a%b); }
14
15 int ksm(int a,int b){
16     int res;
17     for (res=1; b; a=1ll*a*a%mod,b>>=1)
18         if (b & 1) res=1ll*a*res%mod;
19     return res;
20 }
21
22 void pre(){
23     phi[1]=1;
24     rep(i,2,n){
25         if (!pr[i]) p[++tot]=i,phi[i]=i-1;
26         for (int j=1; j<=tot && i*p[j]<=n; j++){
27             int t=i*p[j]; pr[t]=1;
28             if (i%p[j]) phi[t]=phi[i]*(p[j]-1);
29             else { phi[t]=phi[i]*p[j]; break; }
30         }
31     }
32     rep(i,1,n) g[i]=(g[i-1]+1ll*i*i%mod*phi[i])%mod;
33 }
34
35 void init(){
36     bl=sqrt(n); m=(n-1)/bl+1;
37     rep(i,1,n) pos[i]=(i-1)/bl+1,v[i]=1ll*i*i%mod,f[i]=(f[i-1]+v[i])%mod;
38     rep(i,1,m) B[i].l=(i-1)*bl+1,B[i].r=i*bl;
39 }
40
41 void mdf(int x,int k){
42     int r=B[pos[x]].r,d=(k-v[x]+mod)%mod; v[x]=k;
43     if (!k) return;
44     rep(i,x,r) f[i]=(f[i]+d)%mod;
45     rep(i,pos[x]+1,m) c[i]=(c[i]+d)%mod;
46 }
47
48 int que(int x){ return (c[pos[x]]+f[x])%mod; }
49
50 int solve(int n){
51     int ans=0,lst=0,r=0,now=0;
52     for (int i=1; i<=n; i=r+1,lst=now){
53         r=n/(n/i); now=que(r);
54         ans=(ans+1ll*(now-lst+mod)*g[n/i]%mod)%mod;
55     }
56     return ans;
57 }
58
59 int main(){
60     freopen("bzoj4815.in","r",stdin);
61     freopen("bzoj4815.out","w",stdout);
62     scanf("%d%d",&Q,&n); pre(); init();
63     while (Q--){
64         scanf("%d%d%lld%d",&a,&b,&x,&k); x%=mod;
65         int d=gcd(a,b); mdf(d,1ll*x*d%mod*d%mod*ksm((1ll*a*b)%mod,mod-2)%mod);
66         printf("%d\n",solve(k));
67     }
68     return 0;
69 }

posted @ 2018-04-02 18:46  HocRiser  阅读(204)  评论(0编辑  收藏  举报