染色图(数论分块)
题目:
定义一张无向图 G=⟨V,E⟩ 是 k 可染色的当且仅当存在函数 f:V↦{1,2,⋯,k} 满足对于 G 中的任何一条边 (u,v),都有 f(u)≠f(v)。
定义函数 g(n,k) 的值为所有包含 n 个点的无自环、无重边的 k 可染色无向图中的边数最大值。举例来说,g(3,1)=0,g(3,2)=2,g(3,3)=3。
现在给出三个整数 n,l,r,你需要求解: 在l到r区间内g(n,i)的和 mod 998244353
输入
5
3 1 1
3 2 2
5 2 4
10 3 9
1000 123 789
输出
0
2
23
280
332539617
思路,易知当颜色数目为k时,可以将图分成好几个有k个点的块,连成完全图,然后再将这些块连起来,一个一个求的代码是这样的
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<map>
#include<set>
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define x first
#define y second
#define int ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=100100;
int a[maxn],b[maxn];
int n,m,k;
int check1(int i,int mid)
{
int l=0,r=m;
int pos=0;
int tm=0;
while(l<=r)
{
tm=(l+r)>>1;
if(a[i]*b[tm]>=mid) pos=tm,l=tm+1;
else r=tm-1;
}
return pos;
}
int check2(int i,int mid)
{
int l=0,r=m;
int pos=0;
int tm=0;
while(l<=r)
{
//cout<<"l: "<<l<<" r: "<<r<<endl;
tm=(l+r)>>1;
if(a[i]*b[tm]>=mid) pos=tm,r=tm-1;
else l=tm+1;
}
//cout<<"mid: "<<mid<<endl;
//cout<<"pos: "<<pos<<endl;
//cout<<"a[i]: "<<a[i]<<endl;
return (m-pos);
}
int check(int mid)
{
int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]<=0) ans+=check1(i,mid);
else ans+=check2(i,mid);
}
//cout<<"ans: "<<ans<<endl;
return ans;
}
signed main(void)
{
int n,l,r;
while(cin>>n>>l>>r)
{
int ans=0;
for(int i=l;i<=r;i++)
{
int now=l;
int re=n%i;
ans=ans+(i)*(i-1)/2*(n/i);
cout<<"ans1: "<<ans<<endl;
ans=ans+(re)*(re-1)/2;
cout<<"ans2: "<<ans<<endl;
ans=ans+i*(i-1)*(n/i-1)*(n/i)/2;
cout<<"ans3: "<<ans<<endl;
ans=ans+re*(i-1)*(n/i);
cout<<"ans4: "<<ans<<endl;
}
cout<<ans<<endl;
}
return 0;
}
但这样肯定会超时,所以可以将上式子分块求和,其中需要一个操作,就是将n%i写成n-n/i*i。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<map>
#include<set>
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define x first
#define y second
#define int ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=998244353;
int n,nl,nr;
int inv2;
ll mypow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
signed main(void)
{
int t;
scanf("%lld",&t);
inv2=mypow(2,mod-2);
while(t--)
{
scanf("%lld%lld%lld",&n,&nl,&nr);
//n%=mod;
int ans=(n*n%mod-n+mod)%mod*(nr-nl+1)%mod;
ans%=mod;
for(int l=nl,r;l<=nr;l=r+1)
{
r=min(n/(n/l),nr);
int now=n/l;
ans=(ans+(now*now%mod+now)*(r-l+1)%mod*(r+l)%mod*inv2%mod)%mod;
ans=(ans-2*n*now%mod*(r-l+1)%mod+mod)%mod;
}
printf("%lld\n",ans%mod*inv2%mod);
}
return 0;
}