[SDOI2017]数字表格
Decribe:
求
Solution:
显然莫反启动!
这里可以两次分块了,但最后 40 过不了,所以请教了一下大佬 @ScatteredHope。以下是他的推导:
令
然后这一块:
可以
Code:
bool _Start;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
namespace IO
{
#define TP template<typename T>
#define TP_ template<typename T,typename ... T_>
#ifdef DEBUG
#define gc() (getchar())
#else
char buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
#endif
#ifdef DEBUG
void pc(const char &c)
{
putchar(c);
}
#else
char pbuf[1<<20],*pp=pbuf;
void pc(const char &c)
{
if(pp-pbuf==1<<20)
fwrite(pbuf,1,1<<20,stdout),pp=pbuf;
*pp++=c;
}
struct IO{~IO(){fwrite(pbuf,1,pp-pbuf,stdout);}}_;
#endif
TP void read(T &x)
{
x=0;static int f;f=0;static char ch;ch=gc();
for(;ch<'0'||ch>'9';ch=gc())ch=='-'&&(f=1);
for(;ch>='0'&&ch<='9';ch=gc())x=(x<<1)+(x<<3)+(ch^48);
f&&(x=-x);
}
TP void write(T x)
{
if(x<0)
pc('-'),x=-x;
static T sta[35],top;top=0;
do
sta[++top]=x%10,x/=10;
while(x);
while(top)
pc(sta[top--]^48);
}
TP_ void read(T &x,T_&...y){read(x);read(y...);}
TP void writeln(const T x){write(x);pc('\n');}
TP void writesp(const T x){write(x);pc(' ');}
TP_ void writeln(const T x,const T_ ...y){writesp(x);writeln(y...);}
TP inline T max(const T &a,const T &b){return a>b?a:b;}
TP_ inline T max(const T &a,const T_&...b){return max(a,max(b...));}
TP inline T min(const T &a,const T &b){return a<b?a:b;}
TP_ inline T min(const T &a,const T_&...b){return min(a,min(b...));}
TP inline void swap(T &a,T &b){static T t;t=a;a=b;b=t;}
TP inline T abs(const T &a){return a>0?a:-a;}
#undef TP
#undef TP_
}
using namespace IO;
using std::cerr;
using LL=long long;
constexpr int N=1e6+5;
constexpr LL mod=1e9+7;
int prime[N],pr;
LL mu[N],f[N],s[N],invs[N];
bool v[N];
LL qpow(LL a,LL b)
{
LL ans=1ll;a%=mod;
for(;b;b>>=1,a=a*a%mod)
if(b&1)
ans=ans*a%mod;
return ans;
}
void init()
{
mu[1]=1;f[1]=1;
for(int i=2;i<N;i++)
{
f[i]=(f[i-1]+f[i-2])%mod;
if(!v[i])
prime[++pr]=i,mu[i]=-1;
for(int j=1;j<=pr&&prime[j]*i<N;j++)
{
int k=prime[j]*i;
v[k]=1;
if(!(i%prime[j]))
break;
mu[k]=-mu[i];
}
}
for(int i=0;i<N;i++)
s[i]=1;
for(int i=1;i<N;i++)
for(int j=1;j*i<N;j++)
s[j*i]=s[j*i]*(!mu[j]?1:(mu[j]==-1?qpow(f[i],mod-2):f[i]))%mod;
invs[0]=1;
for(int i=1;i<N;i++)
s[i]=(s[i]*s[i-1])%mod,invs[i]=qpow(s[i],mod-2);
}
LL n,m;
bool _End;
int main()
{
// fprintf(stderr,"%.2 MBlf\n",(&_End-&_Start)/1048576.0);
int T;read(T);
init();
while(T--)
{
read(n,m);
if(n>m)
swap(n,m);
LL ans=1ll;
for(LL l=1,r;l<=n;l=r+1)
{
r=min(n/(n/l),m/(m/l));
ans=ans*qpow(s[r]*invs[l-1],(n/l)*(m/l))%mod;
}
writeln(ans);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现