B. XOR Sequences

XOR Sequences

https://codeforces.com/contest/1979/problem/B

题面翻译

给你两个不同的非负整数 x x x y y y。考虑两个无穷序列 a 1 , a 2 , a 3 , … a _ 1, a _ 2, a _ 3, \ldots a1,a2,a3, b 1 , b 2 , b 3 , … b _ 1, b _ 2, b _ 3, \ldots b1,b2,b3,,其中

  • a n = n ⊕ x a _ n = n \oplus x an=nx
  • b n = n ⊕ y b _ n = n \oplus y bn=ny

例如,有了 x = 6 x = 6 x=6 之后,序列 a a a 的前 8 8 8 个元素将如下所示: [ 7 , 4 , 5 , 2 , 3 , 0 , 1 , 14 , … ] [7, 4, 5, 2, 3, 0, 1, 14, \ldots] [7,4,5,2,3,0,1,14,]

你的任务是找出序列 a a a b b b 的最长公共子序列的长度。换句话说,找出最大整数 m m m ,使得 a i = b j , a i + 1 = b j + 1 , … , a i + m − 1 = b j + m − 1 a _ i = b _ j, a _ {i + 1} = b _ {j + 1}, \ldots, a _ {i + m - 1} = b _ {j + m - 1} ai=bj,ai+1=bj+1,,ai+m1=bj+m1 对于某个 i , j ≥ 1 i, j \ge 1 i,j1

题目描述

You are given two distinct non-negative integers $ x $ and $ y $ . Consider two infinite sequences $ a_1, a_2, a_3, \ldots $ and $ b_1, b_2, b_3, \ldots $ , where

  • $ a_n = n \oplus x $ ;
  • $ b_n = n \oplus y $ .

Here, $ x \oplus y $ denotes the bitwise XOR operation of integers $ x $ and $ y $ .

For example, with $ x = 6 $ , the first $ 8 $ elements of sequence $ a $ will look as follows: $ [7, 4, 5, 2, 3, 0, 1, 14, \ldots] $ . Note that the indices of elements start with $ 1 $ .

Your task is to find the length of the longest common subsegment $ ^\dagger $ of sequences $ a $ and $ b $ . In other words, find the maximum integer $ m $ such that $ a_i = b_j, a_{i + 1} = b_{j + 1}, \ldots, a_{i + m - 1} = b_{j + m - 1} $ for some $ i, j \ge 1 $ .

$ ^\dagger $ A subsegment of sequence $ p $ is a sequence $ p_l,p_{l+1},\ldots,p_r $ , where $ 1 \le l \le r $ .

输入格式

Each test consists of multiple test cases. The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers $ x $ and $ y $ ( $ 0 \le x, y \le 10^9, x \neq y $ ) — the parameters of the sequences.

输出格式

For each test case, output a single integer — the length of the longest common subsegment.

样例 #1

样例输入 #1
4
0 1
12 4
57 37
316560849 14570961
样例输出 #1
1
8
4
33554432

提示

In the first test case, the first $ 7 $ elements of sequences $ a $ and $ b $ are as follows:

$ a = [1, 2, 3, 4, 5, 6, 7,\ldots] $

$ b = [0, 3, 2, 5, 4, 7, 6,\ldots] $

It can be shown that there isn’t a positive integer $ k $ such that the sequence $ [k, k + 1] $ occurs in $ b $ as a subsegment. So the answer is $ 1 $ .

In the third test case, the first $ 20 $ elements of sequences $ a $ and $ b $ are as follows:

$ a = [56, 59, 58, 61, 60, 63, 62, 49, 48, 51, 50, 53, 52, 55, 54, \textbf{41, 40, 43, 42}, 45, \ldots] $

$ b = [36, 39, 38, 33, 32, 35, 34, 45, 44, 47, 46, \textbf{41, 40, 43, 42}, 53, 52, 55, 54, 49, \ldots] $

It can be shown that one of the longest common subsegments is the subsegment $ [41, 40, 43, 42] $ with a length of $ 4 $ .

代码

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;

int lowbit(const int &n)
{
    return n & (-n);
}
void solve()
{
    int x, y;
    cin >> x >> y;

    cout << lowbit(x ^ y) << endl; // 输出x和y异或的最低位
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t; // t组数据
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

本题为一道结论题

通过观察输入输出样例,发现输出都是2的n次幂,并且是对应xy异或后的lowbit的大小,故直接写。

% 处理附件1(中文碎片) paper_reconstruction_2d('附件3'); % 处理附件2(英文碎片) paper_reconstruction_2d('附件4'); function paper_reconstruction_2d(folder_path) % 读取所有碎片图像 file_list = dir(fullfile(folder_path, '*.bmp')); num_files = length(file_list); % 检查碎片数量 if num_files ~= 209 error('错误:需要209个碎片(11×19),当前数量:%d', num_files); end % 文件名排序 file_names = {file_list.name}; num_indices = zeros(1, num_files); for i = 1:num_files [~, name, ~] = fileparts(file_names{i}); num_indices(i) = str2double(name); end [~, sorted_idx] = sort(num_indices); file_list = file_list(sorted_idx); % 预处理所有碎片 fragments = cell(1, num_files); fragment_ids = cell(1, num_files); text_ranges = zeros(num_files, 2); % 存储每张碎片的文字顶线和底线 for i = 1:num_files % 读取图像(兼容灰度/RGB) img = imread(fullfile(folder_path, file_list(i).name)); if size(img, 3) == 3 fragments{i} = rgb2gray(img); else fragments{i} = img; end fragment_ids{i} = file_list(i).name(1:end-4); % 检测文字行范围 [top, bottom] = detect_text_lines(fragments{i}); text_ranges(i,:) = [top, bottom]; end % 按文字行位置分组(11行) row_groups = group_by_text_lines(text_ranges); % 对每行碎片进行水平拼接 row_sequences = cell(11, 1); for row = 1:11 if length(row_groups{row}) ~= 19 error('行%d碎片数量不为19', row); end row_sequences{row} = sort_row_fragments(fragments, row_groups{row}); end % 按文字行位置排序行(从上到下) row_order = sort_rows_by_position(row_groups, text_ranges); % 生成最终序列和结果表格 [final_sequence, result_table] = generate_results(row_sequences, row_order, fragment_ids); disp('复原结果表格:'); disp(result_table); % 可视化拼接结果 visualize_reconstruction(fragments, row_sequences, row_order); end %% 核心函数:通过文字行位置分组 function row_groups = group_by_text_lines(text_ranges) % 计算每张碎片的文字行中心位置 centers = mean(text_ranges, 2); % 预分组(11行,按中心高度排序) [~, sorted_idx] = sort(centers); row_groups = cell(11, 1); for i = 1:11 row_groups{i} = sorted_idx((i-1)*19+1 : i*19); end % 验证分组正确性 for row = 1:11 row_ranges = text_ranges(row_groups{row}, :); avg_top = mean(row_ranges(:,1)); avg_bottom = mean(row_ranges(:,2)); % 检查行内文字范围是否一致 if any(abs(row_ranges(:,1) - avg_top) > 1) || ... any(abs(row_ranges(:,2) - avg_bottom) > 1) warning('行%d文字范围不一致,可能需要人工检查', row); end end end %% 核心函数:行间排序(按文字行垂直位置) function row_order = sort_rows_by_position(row_groups, text_ranges) % 计算每行的平均文字行位置 row_positions = zeros(11, 1); for row = 1:11 row_positions(row) = mean(mean(text_ranges(row_groups{row}, :))); end % 按垂直位置排序(从上到下) [~, row_order] = sort(row_positions); end %% 核心函数:生成结果表格 function [sequence, table] = generate_results(row_sequences, row_order, ids) sequence = []; table = cell(11, 19); for row = 1:11 row_idx = row_order(row); for col = 1:19 frag_idx = row_sequences{row_idx}(col); sequence = [sequence, frag_idx]; table{row,col} = ids{frag_idx}; end end end %% 可视化函数 function visualize_reconstruction(fragments, row_sequences, row_order) % 拼接所有行 full_img = []; for row = 1:11 row_img = []; for col = 1:19 row_img = [row_img, fragments{row_sequences{row_order(row)}(col)}]; end full_img = [full_img; row_img]; end % 显示结果 figure; imshow(full_img, []); title('基于文字行对齐的拼接结果'); imwrite(full_img, 'reconstructed_by_lines.bmp'); end %% 文字行检测函数 function [top_line, bottom_line] = detect_text_lines(img) bw_img = imbinarize(img); % bw_img = ~bw_img; horz_proj = sum(bw_img, 2); text_mask = horz_proj > 0.92*max(horz_proj); lines = find(text_mask); if isempty(lines) top_line = 1; bottom_line = size(img, 1); else top_line = min(lines); bottom_line = max(lines); end end %% 单行排序函数(保持不变) function sequence = sort_row_fragments(fragments, frag_indices) num_frags = length(frag_indices); left_edges = cell(1, num_frags); right_edges = cell(1, num_frags); for i = 1:num_frags img = fragments{frag_indices(i)}; left_edges{i} = img(:,1); right_edges{i} = img(:,end); end match_matrix = zeros(num_frags); for i = 1:num_frags for j = 1:num_frags if i == j match_matrix(i,j) = -Inf; else match_matrix(i,j) = -sum(abs(double(right_edges{i}) - double(left_edges{j}))); end end end [~, start] = max(cellfun(@mean, left_edges)); sequence = zeros(1, num_frags); sequence(1) = start; used = false(1, num_frags); used(start) = true; for pos = 2:num_frags [~, best] = max(match_matrix(sequence(pos-1), :)); if ~used(best) sequence(pos) = best; used(best) = true; else [sorted, sorted_idx] = sort(match_matrix(sequence(pos-1), :), 'descend'); for k = 1:num_frags if ~used(sorted_idx(k)) && isfinite(sorted(k)) sequence(pos) = sorted_idx(k); used(sorted_idx(k)) = true; break; end end end end sequence = frag_indices(sequence); end面对2013年全国大学生数学建模竞赛 B题碎纸复原模型与算法,请给出优化代码
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值