[BZOJ4815][CQOI2017]小Q的表格 数论+分块

 1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<cmath>
5 using namespace std;
6 typedef long long ll;
7 const int mod=1e9+7;
8 int inline readint(){
9     int Num;char ch;
10     while((ch=getchar())<'0'||ch>'9');Num=ch-'0';
11     while((ch=getchar())>='0'&&ch<='9') Num=Num*10+ch-'0';
12     return Num;
13 }
14 ll inline readll(){
15     ll Num;char ch;
16     while((ch=getchar())<'0'||ch>'9');Num=ch-'0';
17     while((ch=getchar())>='0'&&ch<='9') Num=Num*10+ch-'0';
18     return Num;
19 }
20 void outint(int x){
21     if(x>=10) outint(x/10);
22     putchar(x%10+'0');
23 }
24 int inline gcd(int x,int y){
25     return !y?x:gcd(y,x%y);
26 }
27 int n,m;
28 int phi[4000010],p[4000010],cnt=0;
30 int f[4000010];
31 bool vis[4000010];
32 void sieve(int n){
33     for(int i=2;i<=n;i++){
34         if(!vis[i]){
35             p[++cnt]=i;
36             phi[i]=i-1;
37         }
38         for(int j=1;p[j]*i<=n;j++){
39             vis[p[j]*i]=true;
40             if(i%p[j]==0){
41                 phi[p[j]*i]=phi[i]*p[j];
42                 break;
43             }
44             phi[p[j]*i]=phi[i]*(p[j]-1);
45         }
46         phi[i]=(1LL*i*i%mod*phi[i]+phi[i-1])%mod;
47         f[i]=(1LL*i*i+f[i-1])%mod;
48     }
49 }
50 void modify(int x,int ad){
51     int l=(x-1)/blk+1,
52         r=min(n,l*blk);
54     for(int i=x;i<=r;i++) f[i]=(f[i]+ad)%mod;
55 }
56 int inline qry(int x){
58 }
59 int main(){
62     f[1]=phi[1]=1;
63     blk=(int)sqrt(n);
64     la=(n-1)/blk+1;
65     sieve(n);
66     for(int i=1;i<=m;i++){
69             g=gcd(a,b),
70             ans=0;
73         x=x/(1LL*(a/g)*(b/g))%mod;
74         modify(g,((x-qry(g)+qry(g-1))%mod+mod)%mod);
75         for(int j=1,now;j<=k;j=now+1){
76             now=k/(k/j);
77             ans=(ans+1LL*(qry(now)-qry(j-1)+mod)%mod*phi[k/j])%mod;
78         }
79         outint(ans);
80         putchar('\n');
81     }
82     return 0;
83 }

posted @ 2017-08-22 21:36  halfrot  阅读(196)  评论(0编辑  收藏  举报