Codeforces1379 B. Dubious Cyrpto(枚举)

题意:

给定l,r,m
要求构造出一组a,b,c,满足:
l<=a,b,c<=r,存在一个正整数n,na+b-c=m

数据范围:l,r<=5e5,m<=1e10

解法:

容易想到枚举,要么枚举a,要么枚举b-c,
但是应该枚举a好做一点。

b-c的范围在l-r到r-l之间
枚举a,令x=m%a,n=m/a,因为x是用取模操作得到的,所以一定>=0

如果n>=1且x<=r-l,那么可以构造出b=x+l,c=l,因为x一定是小于a的,所以x+l一定是<=r的

如果x>r-l,因为b-c的范围在l-r到r-l之间,而这里超出了范围,说明b-c原本是一个负数,正叔是因为取模操作只能得到正数,实际上x=b-c+a,多加了一个a,那么b-c=x-a,设p=x-a就判断p是否>=l-r,如果满足则可以构造出b=r+p,c=r。这种情况不用判断n是否>=1,因为b-c还回去了一个a。

code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=1e5+5;
int l,r,m;
void solve(){
    cin>>l>>r>>m;
    for(int a=l;a<=r;a++){
        int bc=m%a;
        int n=m/a;
        if(n>=1&&bc<=r-l){//bc=b-c
            cout<<a<<' '<<l+bc<<' '<<l<<endl;
            return ;
        }else{//bc=b-c+a
            bc=bc-a;//修正
            if(bc>=l-r){
                cout<<a<<' '<<r+bc<<' '<<r<<endl;
                return ;
            }
        }
    }
}
signed main(){
    ios::sync_with_stdio(0);
    int T;cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44178736/article/details/107476304
今日推荐