hdu 6076 huntian oy Du teach sieve

Observable play table, when GCD (I, J) ==. 1, G C D ( I ^ A - J ^ A , I ^ B - J ^ B ) value of i - j. So, you find that the problem with ab it does not matter ...

Become to seek ΣΣ (ij) [gcd (i, j) == 1] a. There is an obvious conclusion, gcd (i, j) == gcd (ij, i).

Let k ij, becomes

∑(i 1->n)∑k(1->i-1)[gcd(i,k) == 1]。

And because i and i itself, certainly not relatively prime, it becomes

Σ (i 1-> n) Σk (1-> i) [gcd (i, k) == 1]. Then there conclusion, the N 1 ... N and the prime number and, ans = N * phi (N) / 2.

Then it becomes a demand Σphi (i) * i, thought Du prefix and seek to teach sieve. Convolution at the function g (x) = x, on the line.

 

 1 #include <cstdio>
 2 #include <map>
 3 #include <cmath>
 4 using namespace std;
 5 typedef long long ll;
 6 const int MAXN = 1000100,mo = 1e9 + 7,inv2 = (mo + 1) / 2,inv6 = (mo + 1) / 6;
 7 
 8 int T,n,a,b,maxn,phi[MAXN],sum[MAXN],pri[MAXN];
 9 bool vis[MAXN];
10 map<int,int> f;
11 int solve(int n)
12 {
13     if (n <= maxn)
14         return sum[n];
15     if (f.count(n))
16         return f[n];
17     int ans = (ll)n * (n + 1) % mo * (n * 2 + 1) % mo * inv6 % mo;
18     for (int l = 2,r;l <= n;l = r + 1)
19     {
20         r = n / (n / l);
21         int tp = (ll)(l + r) * (r - l + 1) % mo * inv2 % mo;
22         ans -=  (ll)tp * solve(n / l) % mo;
23         if (ans < 0)
24             ans += mo;
25     }
26     return f[n] = ans;
27 }
28 void init()
29 {
30     maxn = 1000000;
31     phi[1] = 1;
32     int tot = 0;
33     for (int i = 2;i <= maxn;i++)
34     {
35         if (vis[i] == false)
36         {
37             pri[++tot] = i;
38             phi[i] = i - 1;
39         }
40         for (int j = 1;j <= tot && i * pri[j] <= maxn;j++)
41         {
42             vis[i * pri[j]] = true;
43             if (i % pri[j] != 0)
44                 phi[i * pri[j]] = phi[i] * (pri[j] - 1);
45             else
46             {
47                 phi[i * pri[j]] = phi[i] * pri[j];
48                 break;
49             }
50         }
51     }
52     for (int i = 1;i <= maxn;i++)
53         sum[i] = (sum[i - 1] + (ll)phi[i] * i % mo) % mo;
54 }
55 
56 int main()
57 {
58     init();
59     for (scanf("%d",&T);T != 0;T--)
60     {
61         scanf("%d%d%d",&n,&a,&b);
62         printf("%d\n",(ll)(solve(n) - 1 + mo) % mo * inv2 % mo);
63     }
64     return 0;
65 }

 

Guess you like

Origin www.cnblogs.com/iat14/p/11407855.html