B - Restore Modulo

该博客讨论了一种编程问题,涉及数组元素按恒定递增或递减的规律变化。博主分享了如何判断数组是否能用常数c和m来描述其变化,并给出了相应的C++代码实现。在数组满足特定条件时,可以找到c和m的值,否则输出-1。文章重点在于理解数组变化模式并找出符合条件的c和m。

B - Restore Modulo

题意
思路
昨天晚上这个题被一些小细节卡了,一直没过去,今天早上才发现原来恒定的递减也是可以的就比如5 4 3 2 1,m可以是无穷大的。
题不难。
如果这个数组是以恒定值递增或者递减的话m是可以无穷大的。
如果这个数组有递增有递减,我们可以从递增的点找到c,必须保证每一个递增的点都是恒定的c,我们从递减的点找到m,也必须每一个递减的点都是恒定的m,然后我们还需要判断这个m和这个数组最大值的比较,如果数组最大值大于m,那就输出-1,如果小于m,那就输出m和c。

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
int a[N];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int maxx=0;
        for(int i=1 ; i<=n ; i++) cin>>a[i],maxx=max(maxx,a[i]);
        if(n==1||n==2)
        {
            cout<<"0"<<endl;
            continue;
        }
        bool flag1=true;
        int v=a[2]-a[1];
        int c,m;
        for(int i=1 ; i<n ; i++)
        {
            if(a[i+1]-a[i]!=v) flag1=false;
            if(a[i+1]>=a[i]) c=a[i+1]-a[i];
        }
        for(int i=1 ; i<n ; i++)
        {
            if(a[i]>a[i+1]) m=a[i]+c-a[i+1];
        }
        if(flag1)
        {
            cout<<"0"<<endl;
            continue;
        }
        bool flag2=true;
        for(int i=1 ; i<n ; i++)
        {
            if(a[i]<=a[i+1]&&c!=a[i+1]-a[i]) flag2=false;
            if(a[i]>=a[i+1]&&m!=a[i]+c-a[i+1]) flag2=false;
        }
        if(!flag2) cout<<"-1"<<endl;
        else
        {
            if(m>c&&m>maxx) cout<<m<<" "<<c<<endl;
            else cout<<"-1"<<endl;
        }
    }
    return 0;
}

--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[62], line 209 207 model.eval() 208 with torch.no_grad(): --> 209 loss, pred_full, ids_restore, target = model(img_tensor) 211 print(f"Loss: {loss.item():.4f}") 213 # --- 还原图像用于可视化 --- File ~\AppData\Roaming\Python\Python313\site-packages\torch\nn\modules\module.py:1775, in Module._wrapped_call_impl(self, *args, **kwargs) 1773 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc] 1774 else: -> 1775 return self._call_impl(*args, **kwargs) File ~\AppData\Roaming\Python\Python313\site-packages\torch\nn\modules\module.py:1786, in Module._call_impl(self, *args, **kwargs) 1781 # If we don't have any hooks, we want to skip the rest of the logic in 1782 # this function, and just call forward. 1783 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks 1784 or _global_backward_pre_hooks or _global_backward_hooks 1785 or _global_forward_hooks or _global_forward_pre_hooks): -> 1786 return forward_call(*args, **kwargs) 1788 result = None 1789 called_always_called_hooks = set() Cell In[62], line 164 160 # === 计算损失:只计算被遮蔽区域 === 161 target = x # 原始补丁值作为目标 162 pred_masked = pred.gather( 163 1, --> 164 ids_restore[:, ids_keep.size(1):, :].unsqueeze(0).expand(B, -1, -1) 165 ) # 被遮蔽的预测: (B, num_mask, 768) 166 target_masked = target.gather( 167 1, 168 ids_restore[:, ids_keep.size(1):, :].unsqueeze(0).expand(B, -1, -1) 169 ) # 被遮蔽的真实值: (B, num_mask, 768) 171 loss = F.mse_loss(pred_masked, target_masked) IndexError: too many indices for tensor of dimension 2
最新发布
11-22
-R restore a previous aborted/crashed session -I ignore an existing restore file (don't wait 10 seconds) -S perform an SSL connect -s PORT if the service is on a different default port, define it here -l LOGIN or -L FILE login with LOGIN name, or load several logins from FILE -p PASS or -P FILE try password PASS, or load several passwords from FILE -x MIN:MAX:CHARSET password bruteforce generation, type "-x -h" to get help -y disable use of symbols in bruteforce, see above -r use a non-random shuffling method for option -x -e nsr try "n" null password, "s" login as pass and/or "r" reversed login -u loop around users, not passwords (effective! implied with -x) -C FILE colon separated "login:pass" format, instead of -L/-P options -M FILE list of servers to attack, one entry per line, ':' to specify port -o FILE write found login/password pairs to FILE instead of stdout -b FORMAT specify the format for the -o FILE: text(default), json, jsonv1 -f / -F exit when a login/pass pair is found (-M: -f per host, -F global) -t TASKS run TASKS number of connects in parallel per target (default: 16) -T TASKS run TASKS connects in parallel overall (for -M, default: 64) -w / -W TIME wait time for a response (32) / between connects per thread (0) -c TIME wait time per login attempt over all threads (enforces -t 1) -4 / -6 use IPv4 (default) / IPv6 addresses (put always in [] also in -M) -v / -V / -d verbose mode / show login+pass for each attempt / debug mode -O use old SSL v2 and v3 -K do not redo failed attempts (good for -M mass scanning) -q do not print messages about connection errors -U service module usage details -m OPT options specific for a module, see -U output for information -h more command line options (COMPLETE HELP) server the target: DNS, IP or 192.168.0.0/24 (this OR the -M option) service the service to crack (see below for supported protocols) OPT some service modules support additional input (-U for module help) 翻译
06-06
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值