题目描述

神犇YY虐完数论后给傻×kAc出了一题

给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对

kAc这种傻×必然不会了,于是向你来请教……

多组输入

输入输出格式

输入格式:

第一行一个整数T 表述数据组数

接下来T行,每行两个正整数,表示N, M

输出格式:

T行,每行一个整数表示第i组数据的结果

输入输出样例

输入样例#1:
复制

2
10 10
100 100
输出样例#1: 复制

30
2791

说明

T = 10000

N, M <= 10000000

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ bool vis[10000002];
int mu[10000002];
ll sum[10000002];
int p[10000002];
int f[10000001];
int tot;
void init() {
mu[1] = 1;
for (int i = 2; i <= 10000000; i++) {
if (!vis[i]) {
p[++tot] = i; mu[i] = -1;
}
for (int j = 1; j <= tot; j++) {
if (i*p[j] > 10000000)break;
vis[i*p[j]] = 1;
if (i%p[j] == 0) {
mu[i*p[j]] = 0; break;
}
else {
mu[i*p[j]] = -mu[i];
}
}
}
for (int i = 1; i <= tot; i++) {
for (int j = 1; j*p[i] <= 10000000; j++) {
f[j*p[i]] += mu[j];
}
}
for (int i = 1; i <= 10000000; i++)
sum[i] = sum[i - 1] + 1ll * f[i];
} int main()
{
// ios::sync_with_stdio(0);
init();
int T = rd();
while (T--) {
int N = rd(), M = rd();
ll ans = 0;
for (int l = 1, r; l <= min(N, M); l = r + 1) {
r = min(N / (N / l), M / (M / l));
ans += 1ll * (sum[r] - sum[l - 1])*(N / l)*(M / l);
}
printf("%lld\n", ans * 1ll);
}
return 0;
}

YY的GCD 数学的更多相关文章

  1. Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)

    2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...

  2. BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】

    2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discu ...

  3. [BZOJ2820]YY的GCD

    [BZOJ2820]YY的GCD 试题描述 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...

  4. bzoj 2820 YY的GCD 莫比乌斯反演

    题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...

  5. 【BZOJ】【2820】YY的GCD

    莫比乌斯反演 PoPoQQQ讲义第二题. 暴力枚举每个质数,然后去更新它的倍数即可,那个g[x]看不懂就算了…… 为什么去掉了一个memset就不T了→_→…… /****************** ...

  6. 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&&bzoj 2820 YY的GCD&&BZOJ 3529 数表)

    首先我们来看一道题  BZOJ 2301 Problem b Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd( ...

  7. 【BZOJ 2820】 YY的GCD (莫比乌斯+分块)

    YY的GCD   Description 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少 ...

  8. 【BZOJ2820】YY的GCD(莫比乌斯反演)

    [BZOJ2820]YY的GCD(莫比乌斯反演) 题面 讨厌权限题!!!提供洛谷题面 题解 单次询问\(O(n)\)是做过的一模一样的题目 但是现在很显然不行了, 于是继续推 \[ans=\sum_{ ...

  9. YY的GCD

    YY的GCD 给出T个询问,询问\(\sum_{i=1}^N\sum_{j=1}^M(gcd(i,j)\in prime)\),T = 10000,N, M <= 10000000. 解 显然质 ...

随机推荐

  1. 解决free -h cached 过大 问题

    //先同步数据 sync //cache 释放: //To free pagecache: echo 1 > /proc/sys/vm/drop_caches //To free dentrie ...

  2. 关于python3 发送邮件

    一:发送文本信息 from email.mime.text import MIMEText from email.header import Header from smtplib import SM ...

  3. JBPM具体应用之decision节点的使用

    JBPM工作流引擎为我们提供了许多的节点应用,每一个节点都有其不同的作用,其中有四个比较常用的节点,他们分别decision,fork,state和task.在本文中我们先介绍decision节点,余 ...

  4. 如何POST一个JSON格式的数据给java接口,获得返回数据

    /** * 模拟post进行url请求 * @param string $url * @param json $post_data */ public function request_post($u ...

  5. Tornado之自定义session

      面向对象基础 面向对象中通过索引的方式访问对象,需要内部实现 __getitem__ .__delitem__.__setitem__方法 #!/usr/bin/env python # -*- ...

  6. A Look at the Razor View Engine in ASP.NET MVC

    The biggest architectural difference that exists between ASP.NET MVC and ASP.NET Web Forms is the ne ...

  7. noip2010 关押罪犯 (vijos1776)

    题目 S城现有两座监狱,一共关押着N名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示某两名罪 ...

  8. mongo状态查看方法

    列举一些常用的mongodb状态查看方法. 1.mongostat 是mongdb自带的状态检测工具, inserts/s 每秒插入次数 query/s 每秒查询次数 update/s 每秒更新次数 ...

  9. linux下配置eclipse环境

    注明:本文为博主原创文章,转载请注明出处 前期准备 (此文使用的是非安装版jdk1.8,你也可以下载版本更低的,而且建议使用1.6版本,66大顺嘛,嘻嘻) 1.点击下载jdk 2.点击下载eclips ...

  10. win32多线程 (一) 线程创建与结束等待

    #include "stdafx.h"#include <Windows.h>#include <iostream> using namespace std ...