Product it again
2024-08-23 04:37:20
题意:求解 $$\prod_{1 \leq i \leq n} \prod_{1 \leq j \leq m} {(i,j)}$$
解法:
满脑子的反演
考虑对于第一个质数 $p$ 的贡献为 $p^{[\frac{n}{p}][\frac{m}{p}] + [\frac{n}{p^2}][\frac{m}{p^2}] ... }$
这样1~n的质数大概有$O(\frac{n}{logn})$,对于每一个质数$O(logn)$,总效率大概为 $O(n)$
#include <iostream>
#include <cstdio>
#include <cstring> #define LL long long
#define N 10000010
#define P 1000000007LL using namespace std; int n, m, tot, prime[N];
bool v[N]; LL qpow(LL x, LL n)
{
LL ans = ;
for(; n; n >>= , x = x * x % P)
if(n & )
ans = ans * x % P;
return ans;
} int main()
{
int T;
for(int i = ; i < N; i++)
if(!v[i])
{
prime[++tot] = i;
for(int j = i+i; j < N; j += i)
v[j] = ;
}
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
if(n > m) swap(n, m);
LL ans = ;
for(int i = ; i <= tot; i++)
if(prime[i] <= n)
{
LL tmp = prime[i];
LL cnt = ;
while(tmp <= n)
{
cnt += (n/tmp) * (m/tmp);
tmp *= prime[i];
}
ans = ans * qpow(prime[i], cnt) % P;
}
printf("%lld\n", ans);
}
}
最新文章
- SpringMVC -- 注解
- DropDownList控件
- 各大搜索引擎智能提示API(JSONP跨域实现自动补全搜索建议)
- JavaScript-cookie是客户端本地,持久存储用户私密数据的文件
- HTML5使用ApplicationCache
- tmux 快捷键
- Min Stack
- online ddl 工具之pt-online-schema-change
- CSS遮罩——如何在CSS中使用遮罩
- [Objective-c 基础 - 2.1] 封装
- qt优点
- swing JTable
- ES6优缺点
- 【转载】Latex定制章节编号格式和计数器
- Guava:好用的java类库 学习小记
- 05-树9 Huffman Codes及基本操作
- TCP如何保证可靠性
- css案例 - mask遮罩层的华丽写法
- 解决错误:Couldn&#39;t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
- Thinkphp3.2版本Controller和Action的访问方法