[반전] LOJ #509.동적 형상 문제

μ2(n)=∑d2|nμ(d)
그다음에 xjb 밀기.
어차피 은퇴했으니까 95점 코드를 버렸어요.
복잡도 분석 및 최적화 공식 문제풀이 참조
#include
#include
#include
#include
using namespace std;
typedef long long ll;

ll n,m;

const int maxn=122474490;
const int N=maxn+5;

int prime[7000000],num;
int mu[N],mu2[N]; 

const int P=10000007;

inline void Pre(int n){
  mu[1]=1; int *vst=mu2;
  for (int i=2;i<=n;i++){
    if (!vst[i]) prime[++num]=i,mu[i]=-1; ll t;
    for (int j=1;j<=num && (t=(ll)i*prime[j])<=n;j++){
      vst[t]=1;
      if (i%prime[j]==0){
    mu[t]=0; break;
      }else
    mu[t]=-mu[i];
    }
  }
  for (int i=1;i<=n;i++) mu2[i]=((bool)mu[i])+mu2[i-1],mu[i]=mu[i-1]+mu[i];
}

inline ll S(ll n){
  if (n<=maxn) return mu2[n];
  int x=sqrt(n); ll ret=0;
  for (int i=1,j;i<=x;i=j+1){
    ll t=n/i/i; j=sqrt(n/t);
    ret+=(mu[j]-mu[i-1])*t;
  }
  return ret;
}

int main(){
  Pre(maxn);
  scanf("%lld%lld",&n,&m);
  if (n>m) swap(n,m); ll ans=0;
  ll last=0,cur;
  for (ll i=1,j;i<=n;i=j+1){
    ll t1=sqrt(n/i),t2=sqrt(m/i);
    j=min(n/(t1*t1),m/(t2*t2));
    cur=S(j);
    ans+=(cur-last)*t1*t2;
    last=cur;
  }
  printf("%lld
"
,ans); return 0; }

좋은 웹페이지 즐겨찾기