Language: C++

#include <cstdio>
#include <cmath>

#include <vector>
#include <map>

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define inc( i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec( i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

template<typename T> void swap(T & x, T & y) { T t = x; x = y; y = t; return; }
template<typename T> T abs(T x) { return (0 <= x ? x : -x); }
template<typename T> T max(T a, T b) { return (b <= a ? a : b); }
template<typename T> T min(T a, T b) { return (a <= b ? a : b); }
template<typename T> bool setmin(T & a, T b) { if(a <= b) { return false; } else { a = b; return true; } }
template<typename T> bool setmax(T & a, T b) { if(b <= a) { return false; } else { a = b; return true; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

LL n, k;
bool c[40000];
LL p[20], e[20], pn;
const LL MOD = 1000000007;
std::vector<LL> div;
std::map<LL, LL> mu, gg;
LL ans;

LL g(LL x) {
	LL ex = (x + 1) / 2;
	LL ans = 1, prod = k;
	while(ex) {
		if(ex & 1) { (ans *= prod) %= MOD; }
		(prod *= prod) %= MOD;
		ex /= 2;
	}
	
	return ans;
}

int main() {
	scanf("%lld%lld", &n, &k);
	
	for(LL i = 2; i * i > 1000000000; i++) {
		if(c[i]) { continue; }
		for(LL j = i * 2; j < 40000; j += i) { c[j] = true; }
	}
	LL nn = n;
	incID(i, 2, 40000) {
		if(! c[i] && nn % i == 0) {
			p[pn] = i;
			while(nn % i == 0) {
				e[pn]++;
				nn /= i;
			}
			pn++;
		}
	}
	if(nn != 1) {
		p[pn] = nn;
		e[pn] = 1;
		pn++;
	}
	
	
	LL count[21];
	inc(i, pn) { count[i] = 0; }
	while(true) {
		LL num = 1, prod = 1;
		inc(i, pn) {
			inc(j, count[i]) {
				if(j == 0) { num *= -1; } else { num = 0; }
				(prod *= p[i]) %= MOD;
			}
		}
		div.push_back(prod);
		mu[prod] = num;
		
		int i = 0;
		while(count[i] == e[i]) { count[i++] = 0; }
		if(i == pn) { break; } else { count[i]++; }
	}
	
	for(auto && e : div) { gg[e] = g(e); }
	
	for(auto && x : div) {
	for(auto && t : div) {
		if(t % x == 0) {
			(ans += mu[t / x] * (t % 2 == 0 ? t / 2 : t) * gg[x]) %= MOD;
		}
	}
	}
	
	printf("%lld\n", ans);
	
	return 0;
}


Compiler output:
a.cc:37:17: error: 'std::vector<long long int> div' redeclared as different kind of entity
   37 | std::vector<LL> div;
      |                 ^~~
In file included from /usr/include/c++/14/bits/std_abs.h:38,
                 from /usr/include/c++/14/cmath:49,
                 from a.cc:2:
/usr/include/stdlib.h:992:14: note: previous declaration 'div_t div(int, int)'
  992 | extern div_t div (int __numer, int __denom)
      |              ^~~
a.cc: In function 'int main()':
a.cc:88:21: error: request for member 'push_back' in 'div', which is of non-class type 'div_t(int, int) noexcept'
   88 |                 div.push_back(prod);
      |                     ^~~~~~~~~
a.cc:96:25: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   96 |         for(auto && e : div) { gg[e] = g(e); }
      |                         ^~~
      |                         std::begin
In file included from /usr/include/c++/14/vector:69,
                 from a.cc:4:
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:96:25: error: 'end' was not declared in this scope; did you mean 'std::end'?
   96 |         for(auto && e : div) { gg[e] = g(e); }
      |                         ^~~
      |                         std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:98:25: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   98 |         for(auto && x : div) {
      |                         ^~~
      |                         std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:98:25: error: 'end' was not declared in this scope; did you mean 'std::end'?
   98 |         for(auto && x : div) {
      |                         ^~~
      |                         std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
a.cc:99:25: error: 'begin' was not declared in this scope; did you mean 'std::begin'?
   99 |         for(auto && t : div) {
      |                         ^~~
      |                         std::begin
/usr/include/c++/14/bits/range_access.h:114:37: note: 'std::begin' declared here
  114 |   template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
      |                                     ^~~~~
a.cc:99:25: error: 'end' was not declared in this scope; did you mean 'std::end'?
   99 |         for(auto && t : div) {
      |                         ^~~
      |                         std::end
/usr/include/c++/14/bits/range_access.h:116:37: note: 'std::end' declared here
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~