2016沈阳网络赛的最后一题,记录一下这个模板,来自某某大神,并不懂这个的原理,先放着当模板。
//
//Created by just_sort 2016/9/12 17:01
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
ll f[340000],g[340000],n;
void init()
{
ll i,j,m;
for(m=1; m*m<=n; ++m)f[m]=n/m-1;
for(i=1; i<=m; ++i)g[i]=i-1;
for(i=2; i<=m; ++i)
{
if(g[i]==g[i-1])continue;
ll x=min(m-1,n/i/i);
for(j=1; j<=x; ++j)
{
if(i*j<m)f[j]-=f[i*j]-g[i-1];
else f[j]-=g[n/i/j]-g[i-1];
}
x=i*i;
for(j=m; j>=x; --j)g[j]-=g[j/i]-g[i-1];
}
}
int main()
{
while(scanf("%I64d",&n)!=EOF)
{
init();
printf("%I64d\n",f[1]);
}
return 0;
}