Summer N Tianle [articles] game --2019 Hang electric multi-school summer training camp (fifth)

Crazy mode is turned on the water solution to a problem, probably will last several times ... until I catch up with the progress so far.

The following explanations include:

\ [1001 【HDU-6624】 【1004 \\ HDU-6627】 【1005 \\ HDU-6628】 【1006 \\ HDU-6629】 【1007 \\ HDU-6630 [\]

[1001] math HDU-6624 fraction

http://acm.hdu.edu.cn/showproblem.php?pid=6624

Find the smallest positive integer \ (B \) satisfies \ (a <b \) and \ (A = BX (MOD \ P) \) .

Reference: https://blog.csdn.net/Sarah_Wang0220/article/details/98771865

Found: \ (0 <A <B \) , \ (A BX = Py + \) ==> \ (0 <= BX-Py A <B \) ==> \ (\ FRAC {X} {P} <\ FRAC {B} {Y} <\ FRAC {P}. 1-X} {\) , requiring minimal \ (B \) .

Iterative method:
\ [\ the aligned the begin {} & \ Because \ X FRAC {P} {} <\ FRAC {B} {Y} <\ FRAC {} {P}. 1-X \ and \ p> x \\ & \ therefore \ frac {p} { x}> 0 \ \ take \ t = \ frac {p} {x} \\ & \ therefore \ frac {p-tx} {x} <\ frac {b-ty} { y} <\ frac {pt ( x-1)} {x-1} \\ & obtained taking the inverse: \ frac {x-1} {pt (x-1)} <\ frac {y} {b-ty } <\ frac {x} { p-tx} \\ & Similarly, by subtracting the integer portion of the left continues \\ & case \ b '= b - ty decreasing \ end {aligned} \]

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

void solve(ll a, ll b, ll &c, ll &d, ll e, ll f) {
    ll t = a/b;
    if(e/f > t) {
        c = t+1;
        d = 1;
        return ;
    }
    a = a - t*b;
    e = e - t*f;
    solve(f, e, d, c, b, a);
    c = c + t*d;
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        ll p, x;
        scanf("%lld%lld", &p, &x);
        ll b, y;
        solve(p, x, b, y, p, x-1);
        printf("%lld/%lld\n", b*x-p*y, b);
    }
    return 0;
}

[1004] math HDU-6627 equation

http://acm.hdu.edu.cn/showproblem.php?pid=6627

Given \ (n-\) and \ (C \) , input \ (n-\) a \ (a_i \) and \ (B_i \) , is calculated for all \ (X \) Solutions such that: \ (\ SUM ^ { {I} _ = n-. 1} | + B_i a_ix | = C \) .

For each of the absolute value of the equation to find \ (x \) range, and then sort the enumeration range, the number of statistical solution.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

const int maxn = 1e5+5;

int n;
ll c;
ll suma[maxn], sumb[maxn];     // 记录 系数/常数 前缀和
map<double, int> vis;

struct node {
    ll a, b;
    double f;
    bool operator < (const node &q) const {
        return f > q.f;
    }
}p[maxn], ans[maxn];

bool cmp(node x, node y) {
    return x.f < y.f;
}

int main() {
    int t;
    scanf("%d", &t);
    suma[0] = sumb[0] = 0; 
    while(t--) {
        vis.clear();
        scanf("%d%lld", &n, &c);
        for(int i = 1; i <= n; i++) {
            scanf("%lld%lld", &p[i].a, &p[i].b);
            p[i].f = -1.0*p[i].b/p[i].a;    // 每组方程等于 0的解
        }
        sort(p+1, p+1+n);
        for(int i = 1; i <= n; i++) {
            suma[i] = suma[i-1] + p[i].a;
            sumb[i] = sumb[i-1] + p[i].b;
        }
        int flag = 0;
        int cnt = 0;
        for(int i = 0; i <= n; i++) {
            ll A = suma[n] - 2*suma[i];     // 当前区间解的 系数 前缀和(分母)
            ll B = sumb[n] - 2*sumb[i];     // 当前区间解的 常数 前缀和
            ll C = c - B;        // 等式常数移到右边获取最终常数(分子)
            if(A == 0) {
                if(C == 0) {    // 无穷解
                    flag = 1;   
                    break;
                }
                else {          // 无解
                    continue;
                }
            }
            else {
                ll temp = __gcd(abs(C), abs(A));
                double mark = 1.0 * C / A;
                if(vis[mark] == 0) {    // 去重
                    if((i==n || mark>p[i+1].f) && (i==0 || mark<=p[i].f)) {     // 边界判定
                        ans[cnt].a = C / temp;
                        ans[cnt].b = A / temp;
                        ans[cnt++].f = mark;
                    }
                }
            }
        }
        if(flag == 1) {
            printf("-1\n");
        }
        else {
            printf("%d", cnt);
            sort(ans, ans+cnt, cmp);
            for(int i = 0; i < cnt; i++) {
                if(ans[i].a*ans[i].b > 0) {
                    printf(" %lld/%lld", abs(ans[i].a), abs(ans[i].b));
                }
                else if(ans[i].a*ans[i].b == 0) {
                    printf(" 0/1");
                }
                else {
                    printf(" -%lld/%lld", abs(ans[i].a), abs(ans[i].b));
                }
            }
            printf("\n");
        }
    }
    return 0;
}

[1005] thinking HDU-6628 permutation 1

http://acm.hdu.edu.cn/showproblem.php?pid=6628

\ (T \) of case, the known sequence is: \ (p_1p_2 ... P_n \) , defined as the difference in sequence: \ (P_1-P_2, P_3-P_2, ..., n--P_n-P_ {}. 1 \) . Given \ (n \) and \ (k \) , find the sequence \ (1,2,3, ..., n \ ) in small differences in the sequence that ranked K, output it. ( \ (2 \ n-Leq \ Leq 20 is \ \ \. 1 \ Leq K \ Leq min (10 ^. 4, n-!) \) )

For \ (n \ leq 8 \) , the direct violence on the line.

For \ (n> 8 \) time, find the law: to lay down \ (n \) this number, the latter is the smallest \ (, 2, 3, ..., the n--1 \) , and then only after 8 bits can be arranged, as \ (8!> 10 ^ 4 \) .

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

const int maxn = 4e4+500;

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8};
int a_8[10] = {1, 2, 3, 4, 5, 6, 7};
int _a_8[10] = {1, 2, 3, 4, 5, 6, 8};
int tot, tot1, tot2, cnt;

struct node {
    int z[10];
    bool operator < (const node x) const {
        for(int i = 0; i < 8; i++) {
            if(z[i] < x.z[i]) {
                return 1;
            }
            else if(z[i] == x.z[i]) {
                continue;
            }
            else {
                return 0;
            }
        }
    }
}temp[maxn], t_8[maxn], _t_8[maxn];

struct NODE {
    int z[10];
    int x[10];
}res[maxn];

bool cmp(NODE nn, NODE mm) {
    for(int i = 0; i < 8; i++) {
        if(nn.x[i] < mm.x[i]) {
            return 1;
        }
        else if(nn.x[i] == mm.x[i]) {
            continue;
        }
        else {
            return 0;
        }
    }
    return 0;
}

void init() {
    do {
        for(int i = 0; i < 8; i++) {
            temp[tot].z[i] = a[i];
        }
        tot++;
    } while(next_permutation(a, a+8));
    sort(temp, temp+tot);
}

void cal(int n) {
    int aa[10];
    for(int i = 0; i < n; i++) {
        aa[i] = i+1;
    }
    do {
        for(int i = 0; i < n; i++) {
            res[cnt].z[i] = aa[i];
        }
        for(int i = 1; i < n; i++) {
            res[cnt].x[i-1] = res[cnt].z[i] - res[cnt].z[i-1];
            // cout << res[cnt].x[i-1] << "  ";
        }
        // cout << endl;
        cnt++;
    } while(next_permutation(aa, aa+n));
    sort(res, res+cnt, cmp);
}

int main() {
    tot = tot1 = tot2 = 0;
    init();
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, k;
        scanf("%d%d", &n, &k);
        if(n <= 8) {
            cnt = 0;
            cal(n);
            for(int i = 0; i < n; i++) {
                printf("%d%c", res[k-1].z[i], i==n-1?'\n':' ');
            }
        }
        else {
            printf("%d ", n);
            for(int i = 1; i < n-8; i++) {
                printf("%d ", i);
            }
            k = k - 1;
            for(int i = 0; i < 8; i++) {
                printf("%d%c", temp[k].z[i]+n-9, i==7?'\n':' ');
            }
        }
    }
    return 0;
}

[1006] Extended KMP HDU-6629 string matching

http://acm.hdu.edu.cn/showproblem.php?pid=6629

Given a string, it is now required to have each of its common prefix and suffix original string, ask how much violence the implementation of the process steps required.

Extended KMP bare title.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

const int maxn = 1e6+5;

char s[maxn];
char T[maxn];
ll nxt[maxn], extend[maxn];

void getnxt(char str[]) {
    int i = 0, j, po;
    int len = strlen(str);
    nxt[0] = len;
    while(str[i] == str[i+1] && i+1 < len) i++; 
    nxt[1] = i;
    po = 1;
    for(i = 2; i < len; i++) {
        if(nxt[i-po]+i < nxt[po]+po)  // case1 可以直接得到next[i]的值
            nxt[i] = nxt[i-po];
        else {  // case2 要继续匹配才能得到next[i]的值
            j = nxt[po] + po - i;
            if(j < 0)   // 如果i>po+next[po],则要从头开始匹配
                j = 0; 
            while(str[j] == str[j+i] && i+j < len) j++; 
            nxt[i] = j;
            po = i;
        }
    }
}
void EXKMP(char s1[], char s2[]) {
    int i = 0, j, po;
    int l1 = strlen(s1);
    int l2 = strlen(s2);
    getnxt(s2);
    while(s1[i] == s2[i] && i < l2 && i < l1) i++;
    extend[0] = i;
    po = 0;
    for(i = 1; i < l1; i++) {
        if(nxt[i-po]+i < extend[po]+po)    //case1 直接可以得到extend[i]的值
            extend[i] = nxt[i-po];
        else {  // case2 要继续匹配才能得到extend[i]的值
            j = extend[po]+po-i;
            if(j < 0)   // 如果i>extend[po]+po则要从头开始匹配
                j = 0;
            while(s1[j+i]==s2[j] && i+j < l1 && j < l2) j++;
            extend[i] = j;
            po = i;
        }
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%s", s);
        strcpy(T, s);
        EXKMP(s, T);
        int n = strlen(s);
        ll ans = 0;
        for(int i = 1; i < n; i++) {
            ans = ans + extend[i];
            if(extend[i] != n-i) {
                ans = ans + 1;
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

[1007] Law HDU-6630 permutation 2

http://acm.hdu.edu.cn/showproblem.php?pid=6630

Given \ (n-, X, Y \) , for \ (1 \) to the \ (n-\) This \ (n-\) number, satisfying: \ (P_1 = X, \ = Y P_2, \ | p_i- {I}. 1-P_ | \ Leq 2 \ (. 1 \ Leq I <n-) \) .

Hit the table to find the law can be.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;

const int maxn = 1e5+5;

ll a[maxn];

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, x, y;
        scanf("%d%d%d", &n, &x, &y);
        if(n == 2 || n == 3) {
            printf("1\n");
            continue;
        }
        a[0] = 0;
        if(x == 1) {
            a[1] = 1;
            a[2] = 1;
            for(int i = 3; i <= y-x; i++) {
                if(i == n-x) {
                    a[i] = (a[i-1] + a[i-2] + a[i-3]) % mod;
                }
                else {
                    a[i] = (a[i-1] + a[i-3]) % mod;
                }
            }
        }
        else if(x == n-1) {
            printf("1\n");
            continue;
        }
        else {
            a[1] = 0;
            a[2] = 1;
            for(int i = 3; i <= y-x; i++) {
                if(i == n-x) {
                    a[i] = (a[i-1] + a[i-2] + a[i-3]) % mod;
                }
                else {
                    a[i] = (a[i-1] + a[i-3]) % mod;
                }
            }            
        }
        printf("%lld\n", a[y-x]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Decray/p/11330796.html