• hdu6814 Tetrahedron 线性求逆元 + 快速幂求逆元


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6814

    pown数组用于线性求逆元,需注意初始化,行号35

    公式,行号37

    理解不了,只能死记硬背了

    参考文献:https://www.cnblogs.com/zjp-shadow/p/7773566.html

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<string>
     5 #include<cstdio>
     6 
     7 #define ll long long
     8 using namespace std;
     9 
    10 const int m = 998244353;
    11 const int maxn = 6e6 + 10;
    12 
    13 int n;
    14 ll sum[maxn];
    15 ll pown[maxn];
    16 
    17 ll fast_pow(ll base,ll k)//base^k // long long - must
    18 {
    19     ll res = 1;
    20     while(k){
    21         if(k & 1){//奇偶 
    22             res *= base % m;
    23         }
    24         base *= base;                
    25         base %= m;res %= m;
    26         
    27         k >>= 1;
    28     }        
    29     return res % m;
    30 }
    31 
    32 int main(){
    33     int T;scanf("%d",&T);    
    34     //pre_solve;
    35     pown[1] = sum[1] = 1;// 
    36     for(int i = 2 ; i <= 6e6 ; i++){
    37         pown[i] = (m - m / i) * pown[m % i] % m;//线性求逆元 
    38         //pown[i] = fast_pow(i % m, m - 2) % m;
    39         sum[i] = (sum[i - 1] + pown[i] * pown[i] % m) % m;//可利用到pown数组结果 
    40         //sum[i] = (sum[i - 1] + fast_pow(i * i % m, m - 2)) % m ;//计算期望
    41     }
    42         
    43     while(T--){
    44         scanf("%d",&n);
    45         printf("%lld
    ",3 * sum[n] * pown[n] % m);
    46     }
    47     // 1/h^2 = 1/a^2 + 1/b^2 + 1/c^2 ; a,b,c [1,n]
    48     // b/a % p = b*a^(p-2) % p 小费马定理 
    49     
    50     return 0;
    51 }
  • 相关阅读:
    Swift _ OC _ 混编
    CoreAnimation 寄宿图
    CoreAnimation 开篇
    iOS 杂笔-26(苹果禁用热更新)
    Swift_TableView(delegate,dataSource,prefetchDataSource 详解)
    Swift_ScrollView _ API详解
    插入排序_c++
    选择排序_c++
    冒泡排序_c++
    Swift_协议
  • 原文地址:https://www.cnblogs.com/ecustlegendn324/p/13587674.html
Copyright © 2020-2023  润新知