快速幂详解[二进制][十进制]+模板

Posted guugle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速幂详解[二进制][十进制]+模板相关的知识,希望对你有一定的参考价值。

xxx

 

 

 

 

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <bitset>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>

#define  endc       std::ios::sync_with_stdio(false); // 关掉c++流
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)
#define  mes(a, b)  memset(a, b, sizeof(a))
#define  isdigit(a) ((a)>=‘0‘&&(a)<=‘9‘)
#define  xiao(a)    (a)>=‘a‘&&(a)<=‘z‘
#define  da(a)      (a)>=‘A‘&&(a)<=‘Z‘
#define  pii        pair<int, int>
#define  lowbit(x)  x & (-x)
#define  fi         first
#define  se         second
#define  lson       id<<1
#define  rson       id<<1|1

typedef unsigned long long int ull;
typedef long long int ll;
const double eps  = 1e-8;
const double pi   = acos(-1.0);
const int    inf  = 0x3f3f3f3f;
const ll     INF  = 1e18 + 8;
const int    maxm = 2e5 + 6;
const int    maxn = 2e5 + 10;
const int    mod  = 100000009;
using namespace std;

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while(!isdigit(ch)){if(ch == ‘-‘) f = -f; ch = getchar();}
    while(isdigit(ch)){x = (x<<3)+(x<<1)+ch-‘0‘; ch = getchar();}
    return x * f;
}

struct node {
    int v, next; ll w;
    node() {}
    node(int v, int n, ll w) : v(v), next(n), w(w) {}
}edge[maxm << 1];
ll dis[maxn], a[maxn];
int head[maxn];
bool vis[maxn];
int tol, n;

void add(int u, int v, ll w) {
    edge[tol] = node(v, head[u], w);  head[u] = tol++;
}

void init() {
    tol = 0;
    mes(head, -1);  mes(vis, false);
    for(int i = 0; i < maxn; ++i)   dis[i] = INF;
}

ll spfa(int s, int t) {
    queue<int> q;
    while(!q.empty()) q.pop();
    dis[s] = 0, vis[s] = true;
    q.push(s);
    while(!q.empty()) {
        int u = q.front(); q.pop();
        vis[u] = false;
        for(int i = head[u]; ~i; i = edge[i].next) {
            node &e = edge[i];
            if(dis[e.v] > dis[u] + e.w) {
                dis[e.v] = dis[u] + e.w;
                if(vis[e.v])    continue;
                vis[e.v] = true;
                q.push(e.v);
            }
        }
    }
    return dis[t];
}

int main() {
    int T;
    // INOPEN;
    // OUTOPEN;
    scanf("%d", &T);
    while(T--) {
        init();
        int n, m, u, v; ll w;
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
        while(m --) {
            scanf("%d%d%lld", &u, &v, &w);
            add(u, v, w);
            add(v, u, w);
        }
        spfa(1, n);
        ll ans = 0; int flag = 0;
        for(int i = 2; i <= n; ++i) {
            if(dis[i] == INF) {
                flag = 1;
                break;
            }
            ans += dis[i] * a[i];
        }
        if(flag)    printf("No Answer
");
        else        printf("%lld
", ans);
    }
    return 0;
}

  

 

以上是关于快速幂详解[二进制][十进制]+模板的主要内容,如果未能解决你的问题,请参考以下文章

[数论+模板] 快速幂及快速幂求逆元算法模板(模板)

模板—十进制快速幂

常用算法模板 | 快速幂

数论——快速幂,模运算及快速幂求逆元

[每日一题2020.06.15]P1226 模板快速幂取余运算

快速幂模板