[构造]triples I

本文针对一个具体的编程问题,即通过位运算求解特定输入条件下输出的最小数量的多个3的倍数,使得这些数的按位或结果等于给定的目标数。文章详细介绍了算法思路,包括如何使用位运算和模运算来确定所需输入数的数量和具体数值,并提供了一段完整的C++代码实现。

链接:https://ac.nowcoder.com/acm/contest/884/D?&headNav=acm&headNav=acm

来源:牛客网

题目描述

Doctor Elephant is testing his new program: output the bitwise or of the numbers inputed.
He has decided to input several multiples of 3 and the output of the program should be his favorite number  aaa.
Because he is lazy, he decided to input as few numbers as possible. He wants you to construct such an input for every  aaa he chose.
It's guaranteed that for every  aaa in the input there is such a solution. If there're multiple solutions you can output any.

输入描述:

There're multiple test cases in a test file.
The first line contains a positive integer  T - the number of test cases.
In each of the following  T lines there is one positive integer aaa.

输出描述:

For each test case output a line. First you need to output the number of numbers in your input, and then you need to output these numbers, separating by spaces.
示例1

输入

复制
2
3
7

输出

复制
1 3
2 3 6

说明

3=3, (3|6)=7

备注:

1≤T≤1051 \leq T \leq 10^51T105, 1≤a≤10181 \leq a \leq 10^{18}1a1018.

思路:

AC代码:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

vector<ll> v[5];

void init(ll a){
  ll now=1;
  while(a){
    if(a&1) v[now%3].push_back(now);
    now<<=1;
    a>>=1;
  }
}

int main()
{
    ll t;scanf("%lld",&t);
    while(t--){
    ll a;scanf("%lld",&a);
    for(ll i=0;i<3;i++) v[i].clear();
    if(a%3==0) printf("1 %lld\n",a);
    else{
        init(a);

        printf("2 ");
        if(a%3==1){
            if(v[1].size()>=2){
                ll p=v[1][0],q=v[1][1];
                printf("%lld %lld\n",a-p,a-q);
            }
            else if(v[1].size()==1){
                ll p=v[1][0],q=v[2][0];
                printf("%lld %lld\n",a-p,p+q);
            }
            else{
                ll p=v[2][0],q=v[2][1],r=v[2][2];
                printf("%lld %lld\n",a-p-q,p+q+r);
            }
        }else{
            if(v[2].size()>=2){
                ll p=v[2][0],q=v[2][1];
                printf("%lld %lld\n",a-p,a-q);
            }
            else if(v[2].size()==1){
                ll p=v[2][0],q=v[1][0];
                printf("%lld %lld\n",a-p,p+q);
            }
            else{
                ll p=v[1][0],q=v[1][1],r=v[1][2];
                printf("%lld %lld\n",a-p-q,p+q+r);
            }
        }
    }
    }
    return 0;
}
View Code

 

 

转载于:https://www.cnblogs.com/lllxq/p/11256345.html

#include <iostream> #include <vector> // 三元组结构体 struct Triple { int row, col; int value; Triple(int r, int c, int v) : row(r), col(c), value(v) {} }; // 稀疏矩阵类 class SparseMatrix { public: int rows, cols, numNonZero; std::vector<Triple> triples; SparseMatrix(int r, int c, int n) : rows(r), cols(c), numNonZero(n) {} // 按列序转置 SparseMatrix transposeByColumn() { SparseMatrix transposed(cols, rows, numNonZero); std::vector<int> num(cols + 1, 0); std::vector<int> position(cols + 1, 0); // 统计每列的非零元素个数 for (int i = 0; i < numNonZero; ++i) { num[triples[i].col]++; } // 计算每列的起始位置 position[1] = 0; for (int i = 2; i <= cols; ++i) { position[i] = position[i - 1] + num[i - 1]; } // 转置 for (int i = 0; i < numNonZero; ++i) { int col = triples[i].col; int pos = position[col]; transposed.triples.push_back(Triple(triples[i].col, triples[i].row, triples[i].value)); position[col]++; } return transposed; } // 按行序转置 SparseMatrix transposeByRow() { SparseMatrix transposed(cols, rows, numNonZero); for (int c = 0; c < cols; ++c) { for (int i = 0; i < numNonZero; ++i) { if (triples[i].col == c) { transposed.triples.push_back(Triple(triples[i].col, triples[i].row, triples[i].value)); } } } return transposed; } // 打印稀疏矩阵 void printSparseMatrix() { for (const auto& triple : triples) { std::cout << "Row: " << triple.row << ", Col: " << triple.col << ", Value: " << triple.value << std::endl; } } }; // 对称矩阵类 class SymmetricMatrix { public: int n; std::vector<int> SA; SymmetricMatrix(int size) : n(size) { SA.resize(n * (n + 1) / 2); } // 存储元素 void store(int i, int j, int value) { if (i >= j) { int index = i * (i + 1) / 2 + j; SA[index] = value; } else { int index = j * (j + 1) / 2 + i; SA[index] = value; } } // 读取元素 int retrieve(int i, int j) { if (i >= j) { int index = i * (i + 1) / 2 + j; return SA[index]; } else { int index = j * (j + 1) / 2 + i; return SA[index]; } } }; // 打印奇数阶魔方阵 void printMagicSquare(int n) { std::vector<std::vector<int>> magicSquare(n, std::vector<int>(n, 0)); int row = 0, col = n / 2; for (int num = 1; num <= n * n; ++num) { magicSquare[row][col] = num; int nextRow = (row - 1 + n) % n; int nextCol = (col + 1) % n; if (magicSquare[nextRow][nextCol] != 0) { row = (row + 1) % n; } else { row = nextRow; col = nextCol; } } // 打印魔方阵 for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { std::cout << magicSquare[i][j] << "\t"; } std::cout << std::endl; } } int main() { // 稀疏矩阵转置测试 SparseMatrix matrix(3, 3, 3); matrix.triples.push_back(Triple(0, 1, 1)); matrix.triples.push_back(Triple(1, 2, 2)); matrix.triples.push_back(Triple(2, 0, 3)); std::cout << "原始魔方阵:" << std::endl; matrix.printSparseMatrix(); SparseMatrix transposedByColumn = matrix.transposeByColumn(); std::cout << "\n行转置:" << std::endl; transposedByColumn.printSparseMatrix(); SparseMatrix transposedByRow = matrix.transposeByRow(); std::cout << "\n列转置:" << std::endl; transposedByRow.printSparseMatrix(); // 对称矩阵测试 SymmetricMatrix symMatrix(3); symMatrix.store(0, 0, 1); symMatrix.store(0, 1, 2); symMatrix.store(0, 2, 3); symMatrix.store(1, 1, 4); symMatrix.store(1, 2, 5); symMatrix.store(2, 2, 6); std::cout << "\n对称矩阵元素(1, 2): " << symMatrix.retrieve(1, 2) << std::endl; // 奇数阶魔方阵测试 std::cout << "\n3x3 魔方阵:" << std::endl; printMagicSquare(3); return 0; }将代码修改一下,使其能实现矩阵的加减法运算并且显示出来
11-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值