YY的GCD 数学
Posted zxyqzy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了YY的GCD 数学相关的知识,希望对你有一定的参考价值。
题目描述
神犇YY虐完数论后给傻×kAc出了一题
给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对
kAc这种傻×必然不会了,于是向你来请教……
多组输入
输入输出格式
输入格式:第一行一个整数T 表述数据组数
接下来T行,每行两个正整数,表示N, M
输出格式:T行,每行一个整数表示第i组数据的结果
输入输出样例
说明
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 ", ans * 1ll); } return 0; }
以上是关于YY的GCD 数学的主要内容,如果未能解决你的问题,请参考以下文章