51nod 范德蒙矩阵

思路:  根据矩阵乘法的定义,G中的第i行第j列的元素 ai,j ,对答案的贡献为 ai,j∗ T中第j行的所有元素之和。

    因此我们可以将T中根据每行的和进行排序。第i行的和可以通过公式 (ai^n−1)/(ai−1)直接得出。
    注意考虑 ai=1,ai=0 以及 ai>MOD 的特殊情况即可。还有就是对于除法取模需要用到逆元(费马小定理)

    一开始没注意除法取模 狂WA 12遍也是心累。。。。。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 
 6 using namespace std;
 7 typedef long long LL;
 8 const LL mod = 1e9 + 7;
 9 const int maxn = 1e5 + 10;
10 LL b[maxn], ans, a[maxn];
11 LL n, m;
12 LL Pow(LL a, LL b)//快速幂
13 {
14     LL ans = 1;
15     while (b) {
16         if (b & 1) {
17             ans *= a;ans %= mod;
18         }
19         a *= a;a %= mod;
20         b >>= 1;
21     }
22     return ans;
23 }
24 int main()
25 {
26     ios::sync_with_stdio(false);
27     while (cin >> n >> m) {
28         for (int i = 1; i <= m; i++) {
29             cin >> a[i];
30         }
31         sort(a + 1, a + m + 1);
32         for (int i = 1; i <= m; i++) {
33             a[i] = (a[i] % mod + mod) % mod;
34             if (a[i] == 0)b[i] = 1;
35             else if (a[i] == 1)b[i] = n;
36             else {                            //费马小定理对除法取模
37                 b[i] = (Pow(a[i], n) - 1 + mod) % mod;
38                 b[i] = b[i] * Pow(a[i] - 1, mod - 2) % mod;
39             }
40         }
41         //以下是求解
42         ans = 0;
43         LL num = (n*(n + 1) / 2) % mod;
44         for (int i = 1; i <= m; i++) {
45             ans = (ans + (num*b[i]) % mod) % mod;
46             num = (num + n * n) % mod;
47         }
48         cout << ans << endl;
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/wangrunhu/p/9451812.html