题解:P10252 线性变换

· · 题解

题目大意

有T行,每行有三个整数 x,a,b,每次 x 可变为 a\times x-b 当 x<0 时停止操作,求过程中 x 的最小值。

优化

当 a=1 时,每次x可变为 x-b ,所以加一个特判。

当 a>1 时,不会发生这种问题。

特别的,不加特判会被这个数据卡爆,只能拿部分分,会TLE三个点。

if(a==1){
    if(b!=0){
        x%=b;
        x-=b;
    }
}

AC code

#include <bits/stdc++.h>
using namespace std;
#define int __int128
#define QAQ ch=getchar();
#define qaq printf("\n")
namespace quickread{
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while (ch<'0'||ch>'9'){if(ch=='-')f=-1;QAQ}
        while (ch>='0'&&ch<='9'){x=x*10+ch-'0';QAQ}
        return x*f;
    }
    inline void write(int x){
        if(x<0){putchar('-');x=-x;}
        if(x>9)write(x/10);
        putchar(x%10+'0');
    }
}
using namespace quickread;
int n,a,x,b;
signed main(){
    n=read();
    while(n--){
        x=read(),a=read(),b=read();
        if(a==1){
            if(b!=0){
                x%=b;
                x-=b;
            }
        }
        else while(a*x-b<x&&x>=0) x=x*a-b;
        write(x),qaq;
    }
    return 0;
}