8.20 internal examination

3 noip increase Answers.

Score: 280.


t1: congruence equation

Topic Portal

noip2012 day2 t1.

The meaning of problems: on request of congruence equation x AX (\ equiv \) \ . 1 \ (\ PMOD {B} \) is the smallest positive integer solution, guaranteed data solvable.

Ideas: (exgcd)[violence]
[Positive solutions] is actually indefinite solution = ax-by equation 1, since the data guarantee a solution, so gcd (a, b) = 1, then x is obtained by expanding in Europe make mediation between 1 ~ b It can be.

Code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
//Mystery_Sky
//
#define INF 0x3f3f3f3f
#define ll long long
#define M 1000100
inline ll read()
{
    ll x=0,f=1;char c=getchar();
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}
ll a, b;
ll x, y, t;
void exgcd(ll a, ll b, ll &t, ll &x, ll &y)
{
    if(!b) {t = a; x = 1; y = 0;}
    else {exgcd(b, a%b, t, y, x); y -= x * (a / b);}
}

int main() {
    a = read(), b = read();
    exgcd(a, b, t, x, y);
    printf("%lld\n", (x + b) % b);
    return 0;
}

t2: information transfer

Topic Portal

noip2015 day1 t2

Meaning of the questions: transfer information between each of n students, each person can only pass the information to their own know only the transmission of information objects, initially only know his own birthday information per person, each round of the game everyone will own before passing the resulting information to their passing objects, when people get their information from someone else's birthday, the game is over. Q up to several rounds of the game.

Ideas: (tarjan algorithm) point to the students, the information transmitted to the transmission object side to build a directed graph, the ring can only find the smallest figure, the output size of the loop. As used herein find tarjan ring.

(This problem with iced tea Kyi should be able to do)

Code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 200010
#define INF 0x3f3f3f3f
int head[MAXN], cnt, ans;
int m, n, sum, dfn[MAXN], low[MAXN];
bool visit[MAXN];
int sumscc, scc[MAXN];
stack <int> s;
struct Edge{
    int to, next;
}edge[MAXN];

inline void add(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

void tarjan(int u) 
{
    dfn[u] = low[u] = ++sum;
    s.push(u);
    visit[u] = true;
    for(int i = head[u]; ~i; i = edge[i].next) {
        int v = edge[i].to;
        if(!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(visit[v]) low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u]) {
        int j, cnt = 0;
        do{
            j = s.top();
            s.pop();
            visit[j] = false;
            cnt++;
        }while(j != u);
        if(cnt != 1) ans = min(ans, cnt);
    }
}

int main() {
    scanf("%d", &n);
    int x;
    memset(head, -1, sizeof(head));
    for(int i = 1; i <= n; i++) {
        scanf("%d", &x);
        add(i, x);
    }
    ans = INF;
    for(int i = 1; i <= n; i++)
        if(!dfn[i]) tarjan(i);
    printf("%d\n", ans);
    return 0; 
}

t3: target-shaped Sudoku

Topic Portal

noip2009 t4

Meaning of the questions: for each square on a normal Sudoku add a score, the higher the closer to the center of the score. When the sum of the scores for the product of a single number, the total score of each bin are completed filled with the figures of the lattice. Output to the highest score given target number of separate shaped

Search + pruning thinking :()

[violence]

Guess you like

Origin www.cnblogs.com/Benjamin-cpp/p/11386653.html