QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#354793#6566. Power of Divisorskevinshan#Compile Error//C++174.1kb2024-03-16 01:37:292024-03-16 01:37:29

Judging History

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

  • [2024-03-16 01:37:29]
  • 评测
  • [2024-03-16 01:37:29]
  • 提交

answer

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define f first
#define s second

#define trav(a,b) for(auto& a:b)
#define sz(a) a.size()

using ul = uint64_t;
using db = double;

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define For(i,a) FOR(i,0,a)

ul modMul(ul a, ul b, const ul mod) {
	ll ret = a*b-mod*(ul)((db)a*b/mod);
	return ret+((ret<0)-(ret>=(ll)mod))*mod; }
ul modPow(ul a, ul b, const ul mod) {
	if (b == 0) return 1;
	ul res = modPow(a,b/2,mod); res = modMul(res,res,mod);
	return b&1 ? modMul(res,a,mod) : res;
}

bool prime(ul n) { // not ll!
	if (n < 2 || n % 6 % 4 != 1) return n-2 < 2;
	ul A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022},
	    s = __builtin_ctzll(n-1), d = n>>s;
	trav(a,A) {   // ^ count trailing zeroes
		ul p = modPow(a,d,n), i = s;
		while (p != 1 && p != n-1 && a%n && i--) p = modMul(p,p,n);
		if (p != n-1 && i != s) return 0;
	}
	return 1;
}

ul pollard(ul n) { // return some nontrivial factor of n
	auto f = [n](ul x) { return modMul(x, x, n) + 1; };
	ul x = 0, y = 0, t = 30, prd = 2, i = 1, q;
	while (t++ % 40 || gcd(prd, n) == 1) { /// speedup: don't take gcd every it
		if (x == y) x = ++i, y = f(x);
		if ((q = modMul(prd, max(x,y)-min(x,y), n))) prd = q;
		x = f(x), y = f(f(y));
	}
	return gcd(prd, n);
}
void factor_rec(ul n, map<ul,int>& cnt) {
	if (n == 1) return;
	if (prime(n)) { ++cnt[n]; return; }
	ul u = pollard(n);
	factor_rec(u,cnt), factor_rec(n/u,cnt);
}

vector<pair<ul,int>> factor(ul n) {
	map<ul,int> cnt; factor_rec(n,cnt);
	return vector<pair<ul,int>>(all(cnt));
}

inline namespace factorBasic {
template<class T> vector<pair<T,int>> factor2(T x) { 
	vector<pair<T,int>> pri;
	for (T i = 2; i*i <= x; ++i) if (x % i == 0) {
		int t = 0;
		while (x % i == 0) x /= i, t ++;
		pri.pb({i,t});
	}
	if (x > 1) pri.pb({x,1});
	return pri;
}
/* Note:
 * number of operations needed s.t.
 *				  phi(phi(...phi(n)...))=1
 * is O(log n).
 * Euler's theorem: a^{\phi(p)}\equiv 1 (mod p), gcd(a,p)=1
 */
ll phi(ll x) {
	trav(a,factor2(x)) x -= x/a.f;
	return x;
}
template<class T> void tour(vector<pair<T,int>>& v, 
	vector<T>& V, int ind, T cur) {
		if (ind == sz(v)) V.pb(cur);
		else {
			T mul = 1;
			For(i,v[ind].s+1) {
				tour(v,V,ind+1,cur*mul);
				mul *= v[ind].f;
			}
		}
	}
template<class T> vector<T> getDivi(T x) {
	auto v = factor2(x);
	vector<T> V; tour(v,V,0,(T)1); sort(all(V));
	return V;
}
}

const ll INF = 1e18;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    if (fopen("input.in", "r")) {
        freopen("input.in", "r", stdin);
        freopen("output.out", "w", stdout);
    }
    ul x; cin>>x;
    vector<pair<ul,int>> y = factor(x);
    // trav(a,y) {
    //     cout << a.f << ' ' << a.s << endl;
    // }
    if(!y.size()) {
        cout << 1 << endl;
        return 0;
    }
    assert(y.size());
    int cur = y[0].s;
    trav(a,y) {
        cur = gcd(cur, a.s);
    }

    // cout << "CUR " << cur << endl;

    // vector<int> tmp = getDivi(cur);

    for(int a=1; a<=cur; a++) {
        ll base = 1;
        ll pw2 = 1;
        // cout << "A " << a << endl;
        trav(b, y) {
            int pw = b.s/cur*a;
            for(int i=0;i<pw;i++) {
                if (base > INF/b.f) {
                    cout << -1 << endl;
                    return 0;
                }
                base *= b.f;
            }
            pw2 *= (1+pw);
        }

        ll ans = 1;
        // assert(base != 1);
        for (int i=0;i<pw2;i++) {
            if (ans > INF/base) {
                cout << -1 << endl;
                return 0;
            }
            ans *= base;
            if(i > 100) {
                cout << -1 << endl;
                return 0;
            }
        }
        if (ans==x) {
            cout << base << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;



    

    
}


Details

answer.code: In function ‘int main()’:
answer.code:110:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  110 |         freopen("input.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
answer.code:111:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  111 |         freopen("output.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/string:43,
                 from /usr/include/c++/13/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
                 from answer.code:4:
/usr/include/c++/13/bits/allocator.h: In destructor ‘std::_Rb_tree<long unsigned int, std::pair<const long unsigned int, int>, std::_Select1st<std::pair<const long unsigned int, int> >, std::less<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >::_Rb_tree_impl<std::less<long unsigned int>, true>::~_Rb_tree_impl()’:
/usr/include/c++/13/bits/allocator.h:184:7: error: inlining failed in call to ‘always_inline’ ‘std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = std::_Rb_tree_node<std::pair<const long unsigned int, int> >]’: target specific option mismatch
  184 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/13/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:152:
/usr/include/c++/13/bits/stl_tree.h:662:16: note: called from here
  662 |         struct _Rb_tree_impl
      |                ^~~~~~~~~~~~~