CF932C Permutation Cycle exgcd(Ax+By=N) +构造

Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.

For given N, A, B, find a permutation P of integers from 1 to N such that for 1 ≤ i ≤ Ng(i) equals either A or B.

更新:求的是最短的周期 f(1) = 6, f(6) = 1构成的Cycle T为A或B // 读错题系列

// f(1, j) = f(6, j-1) = p[6] = 1,此时j - 1 = 1 ==> j = 2 ==> g(1) = 2


求 Ax + By = N,x >= 0, y >= 0
例1,A = 2, B = 5, N = 9,Ax + By = N ==> 2x + 5y = 9 ==> x = 2, y = 1
也就是说,9个数里,有两组2个数,使得g(i) = 2。这两组数分别为2, 1和4, 3
有一组5个数,使得g(i) = 5,这组数为6,7,8,9,5

A = 3, B = 6, N = 9
3x + 6y = 9 ==> x = 1, y = 1
也就是说,9个数里,有一组3个数,使得g(i) = 3。这组数为2,3,1
另有一组6个数,使得g(i) = 6。这组数为5,6,7,8,9,4

A = 4, B = 6, N = 9
4x + 6y = 9,无解

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
#define show(a) cout<<#a<<" = "<<a<<endl
#define show2(b,c) cout<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<endl
#define show3(a,b,c) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<endl
#define show4(a,b,c,d) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<endl
const int maxn = 1005;
#define LOCAL

int flag = false; // no exist
int a, b, n, cnta, cntb;

int main() {
    cin >> n >> a >> b;

    // 求 Ax + By = N,x >= 0, y >= 0
    for(int i = 0;  i * a <= n; i++) { 
        int t = n - a * i;
        if( t%b == 0) {
            cnta = i;
            cntb = t/b;
            flag = true;
            break;
        }
    }// 求出x y

    int pos = 1;
    int lastt = pos;
    if( !flag ) cout << -1 << endl;
    else { // 构造符合条件的排列 
        for(int i = 1; i <= cnta; i++) {
            for(int k = 1; k < a; k++) 
                cout << ++pos << ' ';

            cout << lastt << ' ';
            pos++;
            lastt = pos;
        } // 2 1    4 3

        for(int i = 1; i <= cntb; i++) {
            for(int k = 1; k < b; k++)
                cout << ++pos << ' ';
            
            cout << lastt << ' ';
            pos++;
            lastt = pos;
        } // 6 7 8 9   5
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java程序,用于求解给定方程的解: ```java public class EquationSolver { public static void main(String[] args) { int[] values = {1, 2, 3, 4, 5}; int[] permutation = new int[5]; boolean found = false; // 生成所有的排列 permutation(0, values, permutation, found); // 在每个排列中找到符合方程的解 for (int i = 0; i < permutation.length && !found; i++) { for (int j = i + 1; j < permutation.length && !found; j++) { for (int k = j + 1; k < permutation.length && !found; k++) { for (int m = k + 1; m < permutation.length && !found; m++) { int a = values[permutation[i]]; int b = values[permutation[j]]; int c = values[permutation[k]]; int d = values[permutation[m]]; int result = a * b - c * d + values[4]; if (result == 1) { System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + values[4]); found = true; } } } } } if (!found) { System.out.println("No solution found."); } } // 生成所有的排列 private static void permutation(int index, int[] values, int[] permutation, boolean found) { if (index == permutation.length) { found = false; return; } for (int i = 0; i < values.length && !found; i++) { if (isUnique(i, permutation, index)) { permutation[index] = i; permutation(index + 1, values, permutation, found); } } } // 判断一个数是否在排列中唯一 private static boolean isUnique(int value, int[] permutation, int length) { for (int i = 0; i < length; i++) { if (permutation[i] == value) { return false; } } return true; } } ``` 这个程序使用了递归算法来生成所有可能的排列,并在每个排列中找到符合方程的解。程序的输出如下: ``` a = 2 b = 4 c = 5 d = 1 e = 3 ``` 因此,方程的一个解为2 * 4 - 5 * 1 + 3 = 1。注意,如果没有找到符合方程的解,程序将输出"No solution found."。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值