BZOJ2820:YY的GCD——题解
2024-08-24 10:21:47
http://www.lydsy.com/JudgeOnline/problem.php?id=2820
Description
神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种
傻×必然不会了,于是向你来请教……多组输入
Input
第一行一个整数T 表述数据组数接下来T行,每行两个正整数,表示N, M
Output
T行,每行一个整数表示第i组数据的结果
Sample Input
2
10 10
100 100
10 10
100 100
Sample Output
30
2791
2791
————————————————————————
看hzw的博客吧,他讲的蛮清楚的……
我主要是没有可以写数学公式的东西……
#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=;
int miu[N],su[N],sum[N];
ll f[N];
bool he[N];
void Euler(int n){
int tot=;
miu[]=;
for(int i=;i<=n;i++){
if(!he[i]){
su[++tot]=i;
miu[i]=-;
}
for(int j=;j<=tot;j++){
if(i*su[j]>=n)break;
he[i*su[j]]=;
if(i%su[j]==){
miu[i*su[j]]=;break;
}
else miu[i*su[j]]=-miu[i];
}
}
for(int i=;i<=tot;i++){
int p=su[i];
for(int j=;j*p<=n;j++)f[j*p]+=miu[j];
}
for(int i=;i<=n;i++)f[i]+=f[i-];
return;
}
int main(){
Euler();
int t;
scanf("%d",&t);
while(t--){
int a,b;ll ans=;
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
for(int i=,j;i<=a;i=j+){
j=min(a/(a/i),b/(b/i));
ans+=(f[j]-f[i-])*(a/i)*(b/i);
}
printf("%lld\n",ans);
}
return ;
}
最新文章
- python嵌套函数、闭包与decorator
- jQuery 中时间显示的模版
- Sunny-Code Beta版总结会议
- git项目开发版本控制实践
- 【HDU 1003】 Max Sum
- YII2.0 secruity
- 在shell脚本中使用函数
- pic/at89c2051 programmer
- vmware workstation下的虚拟Linux通过NAT模式共享上网
- POJ 1185 状态压缩DP(转)
- codevs 1047 邮票面值设计
- 神经网络和BP算法推导
- 【Android】android图片轮播
- ios文本框基本使用,以及所有代理方法的作用
- CWnd *和HWnd转换
- 一个操作cvs格式的c++类
- ipython notebook 安装
- TIJ -- 任务间使用管道进行输入/输出
- 基于CentOS搭建VNC远程桌面服务
- LeetCode69.x的平方根