【CF1392】E. Omkar and Duck(构造)

该博客主要讨论如何构造一个矩阵,使得矩阵中每条路径的和都不相同,并给出了一种类似于字典序的构造方法。同时,给出了C++代码实现来解决相关问题,包括矩阵的初始化和路径查找。博客还包含了一个实际的代码示例,用于展示如何根据输入的整数找到对应的路径。
摘要由CSDN通过智能技术生成

题目链接:https://codeforces.com/problemset/problem/1392/E

分析

要构造除每一条路径上的和都不同的矩阵
只需要根据类似字典序的构造方法即可

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define lson (k << 1)
#define rson (k << 1 | 1)
 
const int INF = 0x3f3f3f3f;
const int N = 2e3 + 10;
const int M = 1e6 + 10;
const ll P = 1e9 + 7;
 
int n, q;
ll a[30][30];
ll s;
 
int main() {
    cin>>n;
    for(int k=2;k<=n;k++)
    {
        ll tmp = 1ll << (k - 2);
        for(int i=k,j=1;i>=1&&j<=n;i--,j++)
        {
            if(j % 2) a[i][j] = tmp;
        }
    }
    for(int k=2;k<n;k++)
    {
        ll tmp = 1ll << (k + n - 3);
        for(int i=n,j=k;i>=1&&j<=n;i--,j++)
        {
            if(j % 2) a[i][j] = tmp;
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++) cout<<a[i][j]<<" ";
        cout<<endl;
    }
    cin>>q;
    while(q--)
    {
        cin>>s;
        int x = 1, y = 1;
        int cnt = 0;
        while(x <= n && y <= n)
        {
            cout<<x<<" "<<y<<endl;
            if(x == n && y == n) break;
            if(x + 1 <= n && (s & (1ll << cnt)) == a[x + 1][y])
            {
                x++;
                cnt++;
                continue;
            }
            if(y + 1 <= n && (s & (1ll << cnt)) == a[x][y + 1])
            {
                y++;
                cnt++;
                continue;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值