牛客周赛51

思路:求a mod 上b后的值为amodb, 求gcd(b, amodb)即可

int gcd(int a,int b){
	return b ? gcd(b, a % b) : a;
}

void solve(){
	string a;
	cin >> a;
	int b;
	cin >> b;
	int amodb = 0;
	for(auto c : a){
		amodb = (amodb * 10 + (c - '0')) % b;
	}
	cout << gcd(b, amodb);
}

思路:二分答案,然后,判断当前二分的值符不符合,如果可以走到(n,n)就缩小答案,否则扩大答案。

#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define endl '\n'
#define pq priority_queue
using namespace std;
typedef pair<int, int> pii;

int n, a[550][550], st[550][550];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};

bool check(int mid) {
    queue<pii> q;
    memset(st, 0, sizeof(st));
    if (a[1][1] > mid) return false;
    q.push({1, 1});
    st[1][1] = true;
    
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        
        for (int i = 0; i < 4; i++) {
            int px = x + dx[i], py = y + dy[i];
            if (px < 1 || py < 1 || px > n || py > n)
                continue;
            if (st[px][py])
                continue;
            if (a[px][py] > mid)
                continue;
            q.push({px, py});
            st[px][py] = true;
        }
    }
    return st[n][n];
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
    }

    int l = 0, r = 1e9 + 2;
    while (l + 1 != r) {
        int mid = l + r >> 1;
        if (check(mid))
            r = mid;
        else
            l = mid;
    }
    cout << r << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

相关推荐

  1. 51

    2024-07-15 02:38:03       22 阅读
  2. Round 51

    2024-07-15 02:38:03       20 阅读
  3. Round 51题解

    2024-07-15 02:38:03       19 阅读
  4. Round 50

    2024-07-15 02:38:03       36 阅读
  5. 26

    2024-07-15 02:38:03       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 02:38:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 02:38:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 02:38:03       58 阅读
  4. Python语言-面向对象

    2024-07-15 02:38:03       69 阅读

热门阅读

  1. Android C++系列:Linux网络(五)常见术语

    2024-07-15 02:38:03       19 阅读
  2. DP讨论——适配器、桥接模式等通用理解

    2024-07-15 02:38:03       17 阅读
  3. 昇思第19天

    2024-07-15 02:38:03       20 阅读
  4. MSYS2快速安装和使用

    2024-07-15 02:38:03       22 阅读
  5. 分布式服务基于Zookeeper的分布式锁的实现

    2024-07-15 02:38:03       19 阅读
  6. C++ 入门10:继承和派生类

    2024-07-15 02:38:03       16 阅读
  7. IDC脚本

    IDC脚本

    2024-07-15 02:38:03      18 阅读
  8. 注册sublime text右键打开

    2024-07-15 02:38:03       22 阅读