2818: Gcd
2818: Gcd
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2170 Solved: 979
[Submit][Status][Discuss]
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
Source
题解:一个素数+欧拉函数题,其实就是先求出1-N的欧拉函数,然后枚举1-N之间的质数,然后简单加加就可以了
对了——每次加入的时候在乘2之后记得减1——因为有且仅有一种情况两个数字相同——那就是两个数字刚好等于那个质数,别的没了
1 /************************************************************** 2 Problem: 2818 3 User: HansBug 4 Language: Pascal 5 Result: Accepted 6 Time:1900 ms 7 Memory:156476 kb 8 ****************************************************************/ 9 10 var 11 i,j,k,l,m,n:longint;ans:int64; 12 a,b:array[0..10000005] of longint; 13 c:array[0..10000005] of int64; 14 procedure phi; 15 var i,j:longint; 16 begin 17 m:=0;a[1]:=1; 18 for i:=2 to n do 19 begin 20 if a[i]=0 then 21 begin 22 inc(m); 23 b[m]:=i; 24 a[i]:=i-1; 25 end; 26 for j:=1 to m do 27 begin 28 if (i*b[j])>n then break; 29 if (i mod b[j])=0 then 30 a[i*b[j]]:=a[i]*b[j] 31 else 32 a[i*b[j]]:=a[i]*(b[j]-1); 33 end 34 end; 35 end; 36 begin 37 readln(n);m:=0;phi;ans:=0; 38 for i:=1 to n do c[i]:=c[i-1]+int64(a[i]); 39 for i:=1 to m do inc(ans,c[n div b[i]]*2-1); 40 writeln(ans); 41 readln; 42 end.