您的位置:首页 > 其它

bzoj 2818: Gcd GCD(a,b) = 素数

2014-08-26 23:41 399 查看

2818: Gcd

Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 1566 Solved: 691
[Submit][Status]

Description

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.

Input

一个整数N

Output

如题

Sample Input

4

Sample Output

4

HINT

hint

对于样例(2,2),(2,4),(3,3),(4,2)

1<=N<=10^7

题解:http://wzimpha.sinaapp.com/archives/499#comment-41

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;

typedef long long LL;
const int maxn = 1e7+1;
bool s[maxn];
int prime[maxn],len = 0;
int mu[maxn];
int g[maxn];
int sum1[maxn];
void  init()
{
memset(s,true,sizeof(s));
mu[1] = 1;
for(int i=2; i<maxn; i++)
{
if(s[i] == true)
{
prime[++len]  = i;
mu[i] = -1;
g[i] = 1;
}
for(int j=1; j<=len && (long long)prime[j]*i<maxn; j++)
{
s[i*prime[j]] = false;
if(i%prime[j]!=0)
{
mu[i*prime[j]] = -mu[i];
g[i*prime[j]] = mu[i] - g[i];
}
else
{
mu[i*prime[j]] = 0;
g[i*prime[j]] = mu[i];
break;
}
}
}
for(int i=1; i<maxn; i++)
sum1[i] = sum1[i-1]+g[i];
}

int main()
{
int a;
init();
while(scanf("%d",&a)>0)
{
LL sum = 0;
for(int i=1,la = 0 ; i<=a; i = la+1)
{
la = a/(a/i);
sum = sum + (long long)(sum1[la] - sum1[i-1])*(a/i)*(a/i);
}
printf("%lld\n",sum);
}
return 0;
}


spoj

4491. Primes in GCD Table

Problem code: PGCD

Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greatest common divisor) table! So he now has a table (of height a and width b), indexed from (1,1) to (a,b), and with the value of field (i,j) equal to gcd(i,j). He wants to know how many times he has used prime numbers when writing the table.

Input

First, t ≤ 10, the number of test cases. Each test case consists of two integers, 1 ≤ a,b < 107.

Output

For each test case write one number - the number of prime numbers Johnny wrote in that test case.

Example

Input:

2
10 10
100 100

Output:

30
2791

一样的题,只不过 GCD(x,y) = 素数 .  1<=x<=a ; 1<=y<=b;


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;

typedef long long LL;
const int maxn = 1e7+1;
bool s[maxn];
int prime[maxn],len = 0;
int mu[maxn];
int g[maxn];
int sum1[maxn];
void  init()
{
memset(s,true,sizeof(s));
mu[1] = 1;
for(int i=2;i<maxn;i++)
{
if(s[i] == true)
{
prime[++len]  = i;
mu[i] = -1;
g[i] = 1;
}
for(int j=1;j<=len && (long long)prime[j]*i<maxn;j++)
{
s[i*prime[j]] = false;
if(i%prime[j]!=0)
{
mu[i*prime[j]] = -mu[i];
g[i*prime[j]] = mu[i] - g[i];
}
else
{
mu[i*prime[j]] = 0;
g[i*prime[j]] = mu[i];
break;
}
}
}
for(int i=1;i<maxn;i++)
sum1[i] = sum1[i-1]+g[i];
}

int main()
{
int T,a,b;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&a,&b);
if(a>b) swap(a,b);
LL sum = 0;
for(int i=1,la = 0 ;i<=a;i = la+1)
{
la = min(a/(a/i),b/(b/i));
sum = sum + (long long)(sum1[la] - sum1[i-1])*(a/i)*(b/i);
}
printf("%lld\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: