简化题意可知,实际上题目求得是gcd(i,j)=1(i,j<=n)的数对数目。
线性筛出n大小的欧拉表,求和*2+1即可。需要特判1.
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-8 # define MOD 30031 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } const int N=40005; //Code begin... int phi[N], prime[N], tot; bool check[N]; void getEuler(int n){ mem(check,0); phi[1]=1; tot=0; FOR(i,2,n) { if (!check[i]) prime[tot++]=i, phi[i]=i-1; FO(j,0,tot) { if (i*prime[j]>n) break; check[i*prime[j]]=true; if (i%prime[j]==0) {phi[i*prime[j]]=phi[i]*prime[j]; break;} else phi[i*prime[j]]=phi[i]*(prime[j]-1); } } } int main () { LL ans=0; int n; scanf("%d",&n); if (n==1) {puts("0"); return 0;} --n; getEuler(n); FOR(i,1,n) ans+=phi[i]; ans=ans*2+1; printf("%lld\n",ans); return 0; }
时间: 2024-10-31 13:18:33