Magical little book

1.SPFA forfeit ring:

(1)BFS:

Counter operation can be performed at the time point of each of the pair
if the counter number n that is greater than a negative ring
(queue)
Code

bool SPFA(int s)
{
        int x,y,i,j;    queue<int>q;
        memset(d,127,sizeof(d));    memset(vis,false,sizeof(vis));
        while(!q.empty())  q.pop();
        d[s]=0;
        cnt[s]=1;
        q.push(s);
        vis[s]=true;
        while(!q.empty())
        {
                x=q.front();
                q.pop();
                vis[x]=false;
                for(i=first[x];i;i=next[i])
                {
                        y=v[i];
                        if(d[y]>d[x]+w[i])
                        {
                                d[y]=d[x]+w[i];
                                cnt[y]=cnt[x]+1;
                                if(cnt[y]>n)
                                  return false;
                                if(!vis[y])
                                {
                                        q.push(y);
                                        vis[y]=true;
                                }
                        }
                }
        }
        return true;
}

(2)DFS

If, after this point in the delivery point Chiguo slack point and then to come back to this point shows that slack loop negative
(meaning there stack)
// see the comments in the code of
the code:
void SPFA (int X)
{
int I , J;
VIS [X] = to true;
for (I = First [X]; I; I = [I] Next)
{
J = V [I];
IF (D [J]> D [X] + W [ I])
{// if j is because this place before that x d [x] must be greater than d [j] but where d [j] is greater than the sudden d [x] then this number plus a number a is given negative
// is not required here, but the negative-side ring it will not affect negatively the negative particularly small
IF (VIS [J])
{
in Flag to false =;
return;
}
D [J] = D [X] + W [I];
SPFA (J);
}
}
VIS [X] = to false;
}

2. Fast power Templates

#include<iostream>
#include<cstdio>

using namespace std;

long long b,p,k;
long long a,c,ans = 1;

int main()
{
    cin>>b>>p>>k;
    a = p,c = b;
    while(a > 0)
    {
        if(a & 1 == 1)
        {
            ans *= c;
            ans %= k;
        }
        a /= 2;
        c = (c % k) * (c % k) % k;
    }
    printf("%lld^%lld mod %lld=%lld\n",b,p,k,ans % k);
    return 0;
}

Figure 3. On the small details

What are (1) ring is a self?

Since the ring is from yourself to yourself there is a way this situation usually will not happen, occur when the subject would say in general, it is generally judged to be especially avoid such a situation

4. How to get the multiplicative inverse of a number

The Fermat's little theorem \ (A ^ {(P -. 1)} \ equiv1 \ PMOD {P} \) ( \ (P \) is a prime number) with slight modification that is, \ (aa ^ {p - 2 } \ equiv1 \ PMOD {P} \) , found \ (a ^ {p - 2 } \) that is \ (a \) of the inverse element, can be assisted by flash power demand (see knowledge 2)

Guess you like

Origin www.cnblogs.com/acioi/p/11564874.html