# [bzoj4815] [洛谷P3700] [Cqoi2017] 小Q的表格

### Description###

(1)对任意的正整数a,b,都要满足f(a,b)=f(b,a)；
(2)对任意的正整数a,b,都要

### Input###

1<=m<=10000,1<=a,b,k<=N<=4*106,0<=x<=1018

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

## 简要题解##

$\begin{equation*} f(a,b)=f(gcd(a,b),gcd(a,b)) \cdot \frac{a}{gcd(a,b)} \cdot \frac{b}{gcd(a,b)} \end{equation*}$

## 想法##

$\begin{equation*} f(a,b)=f(gcd(a,b),gcd(a,b)) \cdot \frac{a}{gcd(a,b)} \cdot \frac{b}{gcd(a,b)} \end{equation*}$

$\begin{equation*} ans=\sum\limits_{i=1}^k f(i,i) \cdot [\sum\limits_{x=1}^{k/i} \sum\limits_{y=1}^{k/i} xy \cdot (gcd(x,y)==1)] \end{equation*}$

\begin{equation*} \begin{aligned} g(n)&=\sum\limits_{x=1}^n \sum\limits_{y=1}^n xy \cdot (gcd(x,y)==1)\\ &=2\sum\limits_{x=1}^n \sum\limits_{y=1}^x xy \cdot (gcd(x,y)==1) - \sum\limits_{x=1}^n x^2 \cdot (gcd(x,x)==1) \\ &=2\sum\limits_{x=1}^n \sum\limits_{y=1}^x xy \cdot (gcd(x,y)==1) - 1 \end{aligned} \end{equation*}

\begin{equation*} \begin{aligned} h(n) &=\sum\limits_{y=1}^n ny \cdot (gcd(n,y)==1) \\ &=n \sum\limits_{y=1}^n y \cdot (gcd(n,y)==1) \\ \end{aligned} \end{equation*}

$\begin{equation*} h(n)=\frac{\varphi(n) \cdot n^2}{2} \end{equation*}$

$\begin{equation*} g(n)=2 \sum\limits_{x=1}^n h(x) - 1 \end{equation*}$

$\begin{equation*} ans=\sum\limits_{i=1}^k f(i,i) \cdot g(k/i) \end{equation*}$

## 代码##

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>

#define P 1000000007

using namespace std;

typedef long long ll;
const int N = 4000005;
const int SN = 4005;

ll phi[N];
int prime[N],pnum;
int n,sn,block;
void getphi(){
phi[1]=1;
for(int i=2;i<=n;i++) phi[i]=i-1;
for(int i=2;i<=n;i++){
if(phi[i]==i-1) prime[pnum++]=i;
for(int j=0;j<pnum && (ll)prime[j]*i<=n;j++){
if(i%prime[j]==0){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
phi[i]=(phi[i-1]+(ll)i*i%P*phi[i])%P; //g(i)
}
}

ll query(int x){
if(!x) return 0;
int bl=(x-1)/sn+1;
}
void change(int x,ll c){
int bl=(x-1)/sn+1;
for(int i=x;i<=min(bl*sn,n);i++) num[i]=(num[i]+c)%P;
}
int gcd(int a,int b) { return b ? gcd(b,a%b) : a; }

int main()
{
int m;
scanf("%d%d",&m,&n);
sn=(int)sqrt(n); block=(n-1)/sn+1;
for(int i=1;i<=n;i++) num[i]=(num[i-1]+(ll)i*i%P)%P;
getphi();

int a,b,k,g;
ll ans=0,x,X;
while(m--){
scanf("%d%d%lld%d",&a,&b,&x,&k);
if(a<b) swap(a,b);
g=gcd(a,b);
X=(x/(1ll((ll)a/g)*(b/g)))%P;  //罪魁祸首！！就是这里！！！
change(g,(X-(query(g)-query(g-1)+P)%P+P)%P);
ans=0;
for(int l=1,r;l<=k;l=r+1){
r=k/(k/l);
ans=(ans+(query(r)-query(l-1)+P)%P*phi[k/l]%P)%P;
}
printf("%lld\n",ans);
}

return 0;
}
posted @ 2018-02-23 23:40  秋千旁的蜂蝶~  阅读(232)  评论(0编辑  收藏  举报