砝码和天平

    xiaoxiao2025-04-01  24

    https://ac.nowcoder.com/acm/contest/910/E

    思路:将m转换成w进制的数,因为只有一个,所以系数0,1可以;系数是w-1时就可以两边加一个,一边就可以进一位了;

    要判断a[i]==w!!!因为前一位可能进到当前位了!!!

    #include<algorithm> #include<set> #include<queue> #include<cmath> #include<cstring> #include<iostream> #include<set> #include<vector> #include<queue> #include<cmath> #include<cstdio> #include<map> #include<stack> #include<string> #include<bits/stdc++.h> using namespace std; #define sfi(i) scanf("%d",&i) #define pri(i) printf("%d\n",i) #define sff(i) scanf("%lf",&i) #define ll long long #define ull unsigned long long #define mem(x,y) memset(x,y,sizeof(x)) #define INF 0x3f3f3f3f #define eps 1e-16 #define PI acos(-1) #define lowbit(x) ((x)&(-x)) #define zero(x) (((x)>0?(x):-(x))<eps) #define fl() printf("flag\n") #define MOD(x) ((x%mod)+mod)%mod #define endl '\n' #define pb push_back #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) const int maxn=1e6+9; const int mod=1e9+7; inline ll read() { ll f=1,x=0; char ss=getchar(); while(ss<'0'||ss>'9') { if(ss=='-')f=-1;ss=getchar(); } while(ss>='0'&&ss<='9') { x=x*10+ss-'0';ss=getchar(); } return f*x; } int a[100]; int main() { //FAST_IO; //freopen("input.txt","r",stdin); int t; cin>>t; while(t--) { mem(a,0); ll w,m; bool f=1; scanf("%lld %lld",&w,&m); int k=0; while(m) { a[k++]=m%w; m/=w; } for(int i=0;i<k;i++) { if(a[i]==w) { a[i]-=w; a[i+1]++; } if(a[i]<=1) continue; else if(a[i]==w-1) { a[i]=0; a[i+1]++; } else { f=0; break; } } if(f) { printf("YES\n"); } else { printf("NO\n"); } } return 0; }

     

    最新回复(0)