QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#88241#5503. Euclidean AlgorithmDreamerWA 16551ms3960kbC++174.4kb2023-03-15 18:24:082023-03-15 18:24:11

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-15 18:24:11]
  • 评测
  • 测评结果:WA
  • 用时:16551ms
  • 内存:3960kb
  • [2023-03-15 18:24:08]
  • 提交

answer

#include <bits/stdc++.h>
#define N 10005
#define push(x) (stack[++top] = (x))

    #define ll long long
namespace Poll{
    #define mytz __builtin_ctzll
    inline ll multi(ll &a,ll &b,ll &m){
        __int128 X=a,Y=b,Z=m;
        X=X*Y%Z;
        return X;
    }
    inline ll Irand(ll x){
        return 1ull*((rand()<<15^rand())<<30^(rand()<<15^rand()))%x; //2
    }
    ll KSM(ll a,ll b,ll m){
        ll ans=1LL;
        while(b){
            if(b&1)ans=multi(ans,a,m);
            b>>=1;
            a=multi(a,a,m);
        }
        return ans;
    }
    bool mr(ll x,ll b){
        ll k=x-1;
        while(k){
            ll cur=KSM(b,k,x);
            if(cur!=1&&cur!=x-1)return false;
            if((k&1)==1||cur==x-1)return true;
            k>>=1;
        }
        return true;
    }
    bool prime(ll x){
        if(x==46856248255981ll||x<2)return false;
        if(x==2||x==3||x==7||x==61||x==24251)return true;
        return mr(x,2)&&mr(x,61);
    }
    inline ll m_max(ll a,ll b){return a>b?a:b;}
    ll ans=0,c,mod;
    //inline ll f(ll x){return (multi(x,x,mod)+c)%mod;}
    inline ll gcd(ll a,ll b){
        if(!a)return b;
        if(!b)return a;
        register int t=mytz(a|b);
        a>>=mytz(a);
        do{
            b>>=mytz(b);
            if(a>b){ll t=b;b=a,a=t;}
            b-=a;
        }while(b);
        return a<<t;
    }
    inline ll m_abs(ll x){return x>0?x:-x;}
    // find(ll p){
    //	if(!(p&1))return 2LL;
    //	mod=p;
    //	register ll a,b,x;
    //	while(1){
    //		a=b=Irand(p-2)+1,c=Irand(p-2)+1;
    //		do{
    //			a=f(a);
    //			b=f(f(b));
    //			x=gcd(m_abs(a-b),p);
    //			if(x!=1 and x!=p)return x;
    //		}while(a!=b);
    //	}
    //	return p;
    //}
    ll f(ll x,ll c,ll n)
        {
            return ((__int128)x*x+c)%n;
        }
        ll find(ll x)
        {
            ll s=0,t=0,c=1ll*rand()%(x-1)+1,val=1;
            for(int gol=1;;gol<<=1,s=t,val=1)
            {
                for(int i=1;i<=gol;++i)
                {
                    t=f(t,c,x);
                    val=(__int128)val*abs(t-s)%x;
                    if((i&127)==0)
                    {
                        ll d=gcd(val,x);
                        if(d>1)return d;
                    }
                }
                ll d=gcd(val,x);
                if(d>1)return d;
            }
        }
    long long D(ll x){
        std::vector<ll>p; p.clear();
        if(x==1)return 1;
        register ll u;
        while(!prime(x)&&x>1){
            u=find(x);
            while(u==x||!prime(u))u=find(u);
            while(x%u==0){
                x/=u;
                p.push_back(u);
            }
        }
        if(x!=1)p.push_back(x);
        std::sort(p.begin(), p.end());
        int i,num=2;
        long long res=1;
        for(i=1;i<p.size();++i){
            if(p[i]!=p[i-1]){
                res*=num;
                num=2;
            }
            else ++num;
        }
        return res*num;
    }
}

#define lll __int128

struct pr {
	ll x, y;
	pr (ll x0 = 0, ll y0 = 0) : x(x0), y(y0) {}
	inline pr operator + (const pr &B) const {return pr(x + B.x, y + B.y);}
};

ll n;
int top = 0;
pr stack[N];

/*
inline bool inner(ll x, ll y) {return n < x * y;}

inline bool steep(ll x, pr v) {return (lll)n * v.x <= (lll)x * x * v.y;}*/

ll S1(ll n) {
    if(!n)
        return 0;
    auto inner = [&](ll x,ll y) {return n < x * y;};
    auto steep = [&](ll x, pr v) {return (lll)n * v.x <= (lll)x * x * v.y;};

    top = 0;
	int i, crn = cbrt(n);
	ll srn = sqrt(n), x = n / srn, y = srn + 1;
	ll ret = 0;
	pr L, R, M;
	push(pr(1, 0)); push(pr(1, 1));
	for (; ; ) {
		for (L = stack[top--]; inner(x + L.x, y - L.y); x += L.x, y -= L.y)
			ret += x * L.y + (L.y + 1) * (L.x - 1) / 2;
		if (y <= crn) break;
		for (R = stack[top]; !inner(x + R.x, y - R.y); R = stack[--top]) L = R;
		for (; M = L + R, 1; )
			if (inner(x + M.x, y - M.y)) push(R = M);
			else {
				if (steep(x + M.x, R)) break;
				L = M;
			}
	}
	for (i = 1; i < y; ++i) ret += n / i;
	return ret * 2 - srn * srn;
}
inline ll S2() {
    ll ans = 0;
    for(ll l=1,r;l<=n;l=r+1) {
        r = n/(n/l);
        ans += (n/l) * (S1(r) - S1(l-1));
        ans -= Poll::D(n/l)*(r-l+1);
    }
    return ans;
}
int main() {
    srand(time(0));
	int T;
	for (scanf("%d", &T); T; --T) {scanf("%lld", &n); printf("%lld\n",S2());}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3852kb

input:

3
2
5
14

output:

1
9
62

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 9891ms
memory: 3748kb

input:

3
29107867360
65171672278
41641960535

output:

8921593237533
21300450379732
13136180138425

result:

ok 3 lines

Test #3:

score: -100
Wrong Answer
time: 16551ms
memory: 3960kb

input:

3
90076809172
100000000000
99913139559

output:

30192292781437
33790187414013
33758574429172

result:

wrong answer 1st lines differ - expected: '30192292781431', found: '30192292781437'