hdu 5780 gcd

题意:给定$x, n$满足$1 leq x, n leq 1000000$,求$sum{(x^a-1,x^b-1)}$对$1e9+7$取模后的值,其中$1 leq a, b leq n$。

分析:首先不难有$(x^a - 1, x ^ b - 1) = x^{(a,b)}-1$(证明方法可沿欧几里得定理思路),那么我们只需要考虑$(a,b) = d$即可,设$f(d)$为使得$(a, b) = d$的对数,那么不难有$ans = sum_{d = 1}^{n}{f(d)(x^d-1)}$。下面考虑计算$f(d)$,由于$(a, b) = d$满足$d mid a, d mid b, (frac{a}{d}, frac{b}{d}) = 1$,于是对于满足$1leq a leq b leq n$的序对$(a, b)$,其总数为$sum_{i=1}^{frac{n}{d}}{varphi(i)}$,因为我们可以在互质的对上乘以系数$d$使其映射到对$(a, b)$。那么对于题目中的要求,总数为$2sum_{i=1}^{frac{n}{d}}{varphi(i)}-1$,那么我们就只用到了欧拉函数的前缀和,注意到$f(d)$只与$frac{n}{d}$有关,而右边可以使用等比求和,于是可以分段计算,每段对应的$frac{n}{d}$固定。那么这样做预处理是$O(n)$的,每次询问的复杂度是$O(sqrt{n})$的。代码如下:

  1 #include <algorithm>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <string>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <stack>
  9 #include <ctime>
 10 #include <cmath>
 11 #include <iostream>
 12 #include <assert.h>
 13 #define PI acos(-1.)
 14 #pragma comment(linker, "/STACK:102400000,102400000")
 15 #define max(a, b) ((a) > (b) ? (a) : (b))
 16 #define min(a, b) ((a) < (b) ? (a) : (b))
 17 #define mp std :: make_pair
 18 #define st first
 19 #define nd second
 20 #define keyn (root->ch[1]->ch[0])
 21 #define lson (u << 1)
 22 #define rson (u << 1 | 1)
 23 #define pii std :: pair<int, int>
 24 #define pll pair<ll, ll>
 25 #define pb push_back
 26 #define type(x) __typeof(x.begin())
 27 #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
 28 #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
 29 #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
 30 #define dbg(x) std::cout << x << std::endl
 31 #define dbg2(x, y) std::cout << x << " " << y << std::endl
 32 #define clr(x, i) memset(x, (i), sizeof(x))
 33 #define maximize(x, y) x = max((x), (y))
 34 #define minimize(x, y) x = min((x), (y))
 35 using namespace std;
 36 typedef long long ll;
 37 const int int_inf = 0x3f3f3f3f;
 38 const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
 39 const int INT_INF = (int)((1ll << 31) - 1);
 40 const double double_inf = 1e30;
 41 const double eps = 1e-14;
 42 typedef unsigned long long ul;
 43 typedef unsigned int ui;
 44 inline int readint(){
 45     int x;
 46     scanf("%d", &x);
 47     return x;
 48 }
 49 inline int readstr(char *s){
 50     scanf("%s", s);
 51     return strlen(s);
 52 }
 53 //Here goes 2d geometry templates
 54 struct Point{
 55     double x, y;
 56     Point(double x = 0, double y = 0) : x(x), y(y) {}
 57 };
 58 typedef Point Vector;
 59 Vector operator + (Vector A, Vector B){
 60     return Vector(A.x + B.x, A.y + B.y);
 61 }
 62 Vector operator - (Point A, Point B){
 63     return Vector(A.x - B.x, A.y - B.y);
 64 }
 65 Vector operator * (Vector A, double p){
 66     return Vector(A.x * p, A.y * p);
 67 }
 68 Vector operator / (Vector A, double p){
 69     return Vector(A.x / p, A.y / p);
 70 }
 71 bool operator < (const Point& a, const Point& b){
 72     return a.x < b.x || (a.x == b.x && a.y < b.y);
 73 }
 74 int dcmp(double x){
 75     if(abs(x) < eps) return 0;
 76     return x < 0 ? -1 : 1;
 77 }
 78 bool operator == (const Point& a, const Point& b){
 79     return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
 80 }
 81 double Dot(Vector A, Vector B){
 82     return A.x * B.x + A.y * B.y;
 83 }
 84 double Len(Vector A){
 85     return sqrt(Dot(A, A));
 86 }
 87 double Angle(Vector A, Vector B){
 88     return acos(Dot(A, B) / Len(A) / Len(B));
 89 }
 90 double Cross(Vector A, Vector B){
 91     return A.x * B.y - A.y * B.x;
 92 }
 93 double Area2(Point A, Point B, Point C){
 94     return Cross(B - A, C - A);
 95 }
 96 Vector Rotate(Vector A, double rad){
 97     //rotate counterclockwise
 98     return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
 99 }
100 Vector Normal(Vector A){
101     double L = Len(A);
102     return Vector(-A.y / L, A.x / L);
103 }
104 void Normallize(Vector &A){
105     double L = Len(A);
106     A.x /= L, A.y /= L;
107 }
108 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
109     Vector u = P - Q;
110     double t = Cross(w, u) / Cross(v, w);
111     return P + v * t;
112 }
113 double DistanceToLine(Point P, Point A, Point B){
114     Vector v1 = B - A, v2 = P - A;
115     return abs(Cross(v1, v2)) / Len(v1);
116 }
117 double DistanceToSegment(Point P, Point A, Point B){
118     if(A == B) return Len(P - A);
119     Vector v1 = B - A, v2 = P - A, v3 = P - B;
120     if(dcmp(Dot(v1, v2)) < 0) return Len(v2);
121     else if(dcmp(Dot(v1, v3)) > 0) return Len(v3);
122     else return abs(Cross(v1, v2)) / Len(v1);
123 }
124 Point GetLineProjection(Point P, Point A, Point B){
125     Vector v = B - A;
126     return A + v * (Dot(v, P - A) / Dot(v, v));
127 }
128 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
129     //Line1:(a1, a2) Line2:(b1,b2)
130     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
131            c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
132     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
133 }
134 bool OnSegment(Point p, Point a1, Point a2){
135     return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0;
136 }
137 Vector GetBisector(Vector v, Vector w){
138     Normallize(v), Normallize(w);
139     return Vector((v.x + w.x) / 2, (v.y + w.y) / 2);
140 }
141 
142 bool OnLine(Point p, Point a1, Point a2){
143     Vector v1 = p - a1, v2 = a2 - a1;
144     double tem = Cross(v1, v2);
145     return dcmp(tem) == 0;
146 }
147 struct Line{
148     Point p;
149     Vector v;
150     Point point(double t){
151         return Point(p.x + t * v.x, p.y + t * v.y);
152     }
153     Line(Point p, Vector v) : p(p), v(v) {}
154 };
155 struct Circle{
156     Point c;
157     double r;
158     Circle(Point c, double r) : c(c), r(r) {}
159     Circle(int x, int y, int _r){
160         c = Point(x, y);
161         r = _r;
162     }
163     Point point(double a){
164         return Point(c.x + cos(a) * r, c.y + sin(a) * r);
165     }
166 };
167 int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, std :: vector<Point>& sol){
168     double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
169     double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r;
170     double delta = f * f - 4 * e * g;
171     if(dcmp(delta) < 0) return 0;
172     if(dcmp(delta) == 0){
173         t1 = t2 = -f / (2 * e); sol.pb(L.point(t1));
174         return 1;
175     }
176     t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1));
177     t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2));
178     return 2;
179 }
180 double angle(Vector v){
181     return atan2(v.y, v.x);
182     //(-pi, pi]
183 }
184 int GetCircleCircleIntersection(Circle C1, Circle C2, std :: vector<Point>& sol){
185     double d = Len(C1.c - C2.c);
186     if(dcmp(d) == 0){
187         if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates
188         return 0; //two circles share identical center
189     }
190     if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close
191     if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //too far away
192     double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
193     double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
194     Point p1 = C1.point(a - da), p2 = C1.point(a + da);
195     sol.pb(p1);
196     if(p1 == p2) return 1;
197     sol.pb(p2);
198     return 2;
199 }
200 int GetPointCircleTangents(Point p, Circle C, Vector* v){
201     Vector u = C.c - p;
202     double dist = Len(u);
203     if(dist < C.r) return 0;//p is inside the circle, no tangents
204     else if(dcmp(dist - C.r) == 0){
205         // p is on the circles, one tangent only
206         v[0] = Rotate(u, PI / 2);
207         return 1;
208     }else{
209         double ang = asin(C.r / dist);
210         v[0] = Rotate(u, -ang);
211         v[1] = Rotate(u, +ang);
212         return 2;
213     }
214 }
215 int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
216     //a[i] store point of tangency on Circle A of tangent i
217     //b[i] store point of tangency on Circle B of tangent i
218     //six conditions is in consideration
219     int cnt = 0;
220     if(A.r < B.r) { std :: swap(A, B); std :: swap(a, b); }
221     int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
222     int rdiff = A.r - B.r;
223     int rsum = A.r + B.r;
224     if(d2 < rdiff * rdiff) return 0; // one circle is inside the other
225     double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
226     if(d2 == 0 && A.r == B.r) return -1; // two circle duplicates
227     if(d2 == rdiff * rdiff){ // internal tangency
228         a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
229         return 1;
230     }
231     double ang = acos((A.r - B.r) / sqrt(d2));
232     a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
233     a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
234     if(d2 == rsum * rsum){
235         //one internal tangent
236         a[cnt] = A.point(base);
237         b[cnt++] = B.point(base + PI);
238     }else if(d2 > rsum * rsum){
239         //two internal tangents
240         double ang = acos((A.r + B.r) / sqrt(d2));
241         a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
242         a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
243     }
244     return cnt;
245 }
246 Point ReadPoint(){
247     double x, y;
248     scanf("%lf%lf", &x, &y);
249     return Point(x, y);
250 }
251 Circle ReadCircle(){
252     double x, y, r;
253     scanf("%lf%lf%lf", &x, &y, &r);
254     return Circle(x, y, r);
255 }
256 //Here goes 3d geometry templates
257 struct Point3{
258     double x, y, z;
259     Point3(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
260 };
261 typedef Point3 Vector3;
262 Vector3 operator + (Vector3 A, Vector3 B){
263     return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
264 }
265 Vector3 operator - (Vector3 A, Vector3 B){
266     return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
267 }
268 Vector3 operator * (Vector3 A, double p){
269     return Vector3(A.x * p, A.y * p, A.z * p);
270 }
271 Vector3 operator / (Vector3 A, double p){
272     return Vector3(A.x / p, A.y / p, A.z / p);
273 }
274 double Dot3(Vector3 A, Vector3 B){
275     return A.x * B.x + A.y * B.y + A.z * B.z;
276 }
277 double Len3(Vector3 A){
278     return sqrt(Dot3(A, A));
279 }
280 double Angle3(Vector3 A, Vector3 B){
281     return acos(Dot3(A, B) / Len3(A) / Len3(B));
282 }
283 double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
284     return abs(Dot3(p - p0, n));
285 }
286 Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
287     return p - n * Dot3(p - p0, n);
288 }
289 Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
290     Vector3 v = p2 - p1;
291     double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
292     return p1 + v * t;//if t in range [0, 1], intersection on segment
293 }
294 Vector3 Cross(Vector3 A, Vector3 B){
295     return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
296 }
297 double Area3(Point3 A, Point3 B, Point3 C){
298     return Len3(Cross(B - A, C - A));
299 }
300 class cmpt{
301 public:
302     bool operator () (const int &x, const int &y) const{
303         return x > y;
304     }
305 };
306 
307 int Rand(int x, int o){
308     //if o set, return [1, x], else return [0, x - 1]
309     if(!x) return 0;
310     int tem = (int)((double)rand() / RAND_MAX * x) % x;
311     return o ? tem + 1 : tem;
312 }
313 void data_gen(){
314     srand(time(0));
315     freopen("in.txt", "w", stdout);
316     int kases = 10;
317     printf("%d
", kases);
318     while(kases--){
319         int sz = 2e4;
320         int m = 1e5;
321         printf("%d %d
", sz, m);
322         FOR(i, 1, sz) printf("%d ", Rand(100, 1));
323         printf("
");
324         FOR(i, 1, sz) printf("%d ", Rand(1e9, 1));
325         printf("
");
326         FOR(i, 1, m){
327             int l = Rand(sz, 1);
328             int r = Rand(sz, 1);
329             int c = Rand(1e9, 1);
330             printf("%d %d %d %d
", l, r, c, Rand(100, 1));
331         }
332     }
333 }
334 
335 struct cmpx{
336     bool operator () (int x, int y) { return x > y; }
337 };
338 const int mod = 1e9 + 7;
339 const int maxn = 1e6 + 10;
340 ll phi[maxn];
341 void init_miu(){
342     clr(phi, 0);
343     phi[1] = 1;
344     FOR(i, 2, maxn - 1) if(!phi[i]){
345         for(int j = i; j < maxn; j += i){
346             if(!phi[j]) phi[j] = j;
347             phi[j] = phi[j] / i * (i - 1);
348         }
349     }
350     FOR(i, 1, maxn - 1) phi[i] = (phi[i] + phi[i - 1]) % mod;
351 }
352 ll power(ll a, ll p, ll mod){
353     ll res = 1;
354     a %= mod;
355     while(p){
356         if(p & 1) res = res * a % mod;
357         p >>= 1;
358         a = a * a % mod;
359     }
360     return res;
361 }
362 
363 ll cal2(int x, int n){
364     ll ans = 0;
365     int p = 1;
366     while(p <= n){
367         int np = n / (n / p);
368         int delta = np - p + 1;
369         ll cnt = (2 * phi[n / p] + mod - 1) % mod;
370         ll lhs = 0;
371         if(x > 1){
372             lhs = power(x, p, mod) * (power(x, delta, mod) + mod - 1) % mod;
373             lhs = lhs * power(x - 1, mod - 2, mod) % mod;
374             lhs = (lhs - delta + mod) % mod;
375         }
376         ans = (ans + lhs * cnt % mod) % mod;
377         p = np + 1;
378     }
379     return ans;
380 }
381 int main(){
382     //data_gen(); return 0;
383     //C(); return 0;
384     int debug = 0;
385     if(debug) freopen("in.txt", "r", stdin);
386     //freopen("out.txt", "w", stdout);
387     init_miu();
388     int T = readint();
389     while(T--){
390         int x = readint(), n = readint();
391         printf("%lld
", cal2(x, n));
392     }
393     return 0;
394 }
code:

 当然本题我用莫比乌斯反演也勉强通过了(用了900多ms,仅仅通过了1次),使用了两次分段求和,每次询问的复杂度介于$O(sqrt{n})$与$O(n)$之间。虽然不是正解,仍然可以看出欧拉函数与莫比乌斯函数之间关系相通。

原文地址:https://www.cnblogs.com/astoninfer/p/5772431.html