SPOJ GCDMAT - GCD OF MATRIX
xiezheyuan · · 题解
[$\def\noteh{44pt}\def\notetitlepos{24pt}\def\notetitleh{20pt}\def\noteSetT#1{ \def\notetitle{\textsf{\textbf{#1}}}}\def\noteText#1{ \def\txt{#1}}\def\noteSide#1{ \def\notew{#1}}\def\notetitleoffest{37pt}\def\noteNote{ \def\notech{\kern{-1.5pt}✒\kern{0.5pt}} \noteSetT{注意} \def\notelcol{#448AFF}\def\notetcol{#ECF3FF}}\def\noteAbstract{ \def\notech{\kern{-2pt}\char"d83d\char"ddd2\kern{1.5pt}} \noteSetT{摘要} \def\notelcol{#00B0FF}\def\notetcol{#E5F7FF}}\def\noteInfo{ \def\notech{\kern{2pt}\bf{i}\kern{5.5pt}} \noteSetT{提示} \def\notelcol{#00B8D4}\def\notetcol{#E5F8FB}}\def\noteTip{ \def\notech{\kern{-2pt}\char"d83d\char"dca1} \noteSetT{提示} \def\notelcol{#00BFA5}\def\notetcol{#E5F8F6}}\def\noteOk{ \def\notech{\kern{-2.0pt}\bf{√}\kern{2.4pt}} \noteSetT{示例\kern{-0.5pt}} \def\notelcol{#00C853}\def\notetcol{#E5F9ED}}\def\noteQ{ \def\notech{\kern{0.5pt}\textbf{?}\kern{4.5pt}} \noteSetT{问题} \def\notelcol{#64DD17}\def\notetcol{#EFFCE7}}\def\noteWarn{ \def\notech{\kern{-1pt}⚠\kern{2.8pt}} \noteSetT{警告} \def\notelcol{#FF9100}\def\notetcol{#FFF4E5}}\def\noteFail{ \def\notech{\kern{-2.4pt}\Large\bf{×}\kern{1.6pt}} \noteSetT{失败} \def\notelcol{#FF5252}\def\notetcol{#FFEDED}}\def\noteDanger{ \def\notech{\kern{-5pt}⚡} \noteSetT{危险} \def\notelcol{#FF1744}\def\notetcol{#FFE7EC}}\def\noteBug{ \def\notech{\kern{-4.5pt}\char"d83d\char"dc1e\kern{-0.5pt}} \noteSetT{错误} \def\notelcol{#F50057}\def\notetcol{#FEE5EE}}\def\noteCite{ \def\notech{\kern{1pt}\textbf{"}\kern{3.2pt}} \noteSetT{引述} \def\notelcol{#9E9E9E}\def\notetcol{#F5F5F5}}\noteCite\def\noteinfo{}\def\drawnote{\color{\notelcol}\rule{2pt}{\noteh}\color{\notetcol}\rule[\notetitlepos]{\notew}{\notetitleh}\color{#e8e8e8}\rule{0.5pt}{\noteh}\color{#f5f5f5}\rule{0.5pt}{\noteh}\color{#fafafa}\rule{0.5pt}{\noteh}\kern{-\notew}\kern{-1.5pt}\def\drawx#1#2{\color{#1}\rule[#2]{\notew}{0pt}\kern{-\notew}}\drawx{#bfbfbf}{0pt}\drawx{#d6d6d6}{-0.5pt}\drawx{#ececec}{-1.0pt}\drawx{#f8f8f8}{-1.5pt}\color{black}\raisebox{\notetitlepos}{ \raisebox{6pt}{ \kern{-1pt} \color{\notelcol}\large{\notech} \raisebox{1.5pt}{ \color{#404040}\footnotesize \kern{-2pt}\notetitle } }}\kern{-\notetitleoffest}\raisebox{10pt}{ \footnotesize \sf{\txt}}}\def\build{\drawnote\newline} \noteSide{200pt}\noteText{也许有更好的阅读体验,请点击此处} \noteInfo\build
思路
这是一道欧拉反演题。
首先我们可以转换为求下面的式子:
为什么,因为可以通过差分的思想将题目中的式子改为
下面设
定理 1(欧拉反演公式):
证明:
推论 1:
证明:由欧拉反演公式
然后代入原式(以下过程省略模数):
改为先枚举
将右边的式子化简得:
预处理
时间复杂度预处理
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int phi[10000005],vis[10000005],prime[10000005],sum[10000005],tot;
const int mod = 1e9+7;
int M(const int x){
return (x%mod+mod)%mod;
}
void sieve(){
phi[1]=1;
for(int i=2;i<=50000;i++){
if(!vis[i]){
prime[++tot]=i;
phi[i]=i-1;
}
for(int j=1;j<=tot&&i*prime[j]<=50000;j++){
vis[i*prime[j]]=1;
if(!(i%prime[j])){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
phi[i*prime[j]]=phi[i]*phi[prime[j]];
}
}
for(int i=1;i<=50000;i++){
sum[i]=M(sum[i-1]+phi[i]);
}
}
int solve(int a,int b) {
int ans=0;
if (a>b) swap(a,b);
for (int l=1,r=0;l<=a;l=r+1) {
r=min(a/(a/l),b/(b/l));
ans=M(ans+M(M(M(sum[r]-sum[l-1])*(a/l))*(b/l)));
}
return ans;
}
int n,m,T;
signed main() {
sieve();
cin>>T>>n>>m;
while (T--) {
int i1,j1,i2,j2;
cin>>i1>>j1>>i2>>j2;
cout<<M(solve(i2,j2)-solve(i1-1,j2)-solve(i2,j1-1)+solve(i1-1,j1-1))<<'\n';
}
return 0;
}