牛客练习赛35 C题 函数的魔法 (Floyd)

博客给出题目链接,涉及Floyd变形,有dis[i][F(i)]=1、dis[i][G(i)]=1、dis[i][i]=0的设定,最终要求解min(dis[F(A)][B],dis[G(A)][B])。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:https://ac.nowcoder.com/acm/contest/326/C

Floyd变形,dis[i][F(i)]=1,dis[i][G(i)]=1,dis[i][i]=0

最后求min(dis[F(A)][B],dis[G(A)][B])

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
static const int MAX_N = 235;
static const ll Mod = 233;
static const int N = 105;
static const int INF = 0x3f3f3f3f;
int dis[MAX_N][MAX_N];
int fun1(int x) { return x * x % Mod * (x + 1) % Mod; }
int fun2(int x) { return x * x % Mod * (x - 1) % Mod; }
void solve() {
    memset(dis, INF, sizeof(dis));
    for (int i = 0; i < 233; ++i) {
        dis[i][fun1(i)] = 1;
        dis[i][fun2(i)] = 1;
        dis[i][i] = 0;
    }
    for (int i = 0; i < 233; ++i) {
        for (int j = 0; j < 233; ++j) {
            for (int k = 0; k < 233; ++k) {
                if (dis[j][i] >= INF || dis[i][k] >= INF) continue;
                dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
            }
        }
    }
}
int main() {
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
    int T;
    solve();
    scanf("%d", &T);
    while (T--) {
        ll A, B;
        scanf("%lld%lld", &A, &B);
        if (A == B) puts("0");            //A,B相等必须首先判断
        else if (B >= 233) puts("-1");    
        else {
            A %= 233;
            int res = INF;
			if(dis[fun1(A)][B] < INF) res = min(res, dis[fun1(A)][B] + 1);
			if(dis[fun2(A)][B] < INF) res = min(res, dis[fun2(A)][B] + 1);
			if(res >= INF) puts("-1");
			else printf("%d\n", res);
        }
    }
    return 0;
}

 

牛客练习赛目通常需要实际编程操作才能解答,但我会尽力提供指导。如果你想了解C语言解法,这里假设前三分别是基础的数值计算或数据结构操作: 1. 如果是简单的算术,比如求两数之和,你可以这样写: ```c #include <stdio.h> int main() { int a, b; printf("请输入两个整数:"); scanf("%d %d", &a, &b); int sum = a + b; printf("它们的和是:%d\n", sum); return 0; } ``` 2. 对于查找或排序算法,如查找数组中的最大值,可以这样做: ```c #include <stdio.h> int find_max(int arr[], int n) { int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } int main() { int array[] = {5, 8, 3, 9, 1}; int size = sizeof(array) / sizeof(array[0]); int max_value = find_max(array, size); printf("最大值是:%d\n", max_value); return 0; } ``` 3. 如果涉及字符串处理,比如比较两个字符串,可以这样实现: ```c #include <stdio.h> #include <string.h> int string_compare(const char *str1, const char *str2) { return strcmp(str1, str2); } int main() { char str1[] = "Hello"; char str2[] = "World"; int result = string_compare(str1, str2); if (result < 0) { printf("str1 %s str2\n", "is less than"); } else if (result > 0) { printf("str1 %s str2\n", "is greater than"); } else { printf("str1 %s str2\n", "equals"); } return 0; } ``` 请注意,这只是一个简化的例子,实际比赛中的目可能会更复杂,你还需要考虑错误处理、输入验证等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值