zoukankan      html  css  js  c++  java
  • Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs

    C. Vanya and Scales

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/552/problem/C

    Description

    Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.

    Input

    The first line contains two integers w, m (2 ≤ w ≤ 109, 1 ≤ m ≤ 109) — the number defining the masses of the weights and the mass of the item.

    Output

    Print word 'YES' if the item can be weighted and 'NO' if it cannot.

    Sample Input

    3 7

    Sample Output

    YES

    HINT

    题意

    给你100个砝码,第i个砝码质量是w^i,然后问你能不能在有m的情况下,左边和右边都放砝码,使得这个天平平衡

    题解:

    dfs直接暴力就好了……

    对于这个砝码来说,只有3种选择,放左边,不放,放右边

    由于2和3直接输出yes,所以答案也就没多少了

    代码:

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)  
    #define maxn 2000001
    #define mod 10007
    #define eps 1e-5
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    ll w,m;
    int flag;
    void dfs(ll a,ll b,ll c)
    {
    
        if(c==m)
        {
            flag=1;
            return;
        }
        if(flag||a==-1)
            return;
        if(abs(b)<=m*w)
        {
            dfs(1,b*w,c+b);
            dfs(1,b*w,c-b);
            dfs(1,b*w,c);
        }
    
        return;
    }
    int main()
    {
        flag=0;
        w=read(),m=read();
        if(w<=3)
        {
            cout<<"YES"<<endl;
            return 0;
        }
        dfs(1,1,0);
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
  • 相关阅读:
    【全过程详解】如何恢复联想隐藏的内存(分区教程)
    【全过程详解】如何安装最纯净、稳定、无更新的win10系统(下载+U盘制作+安装+win10激活+驱动更新)
    第一次博客
    Spring boot 直接访问templates中html文件
    Spring boot + mybatis + orcale
    Spring boot+ maven + thymeleaf + HTML 实现简单的web项目
    HTML 中点击<a>标签,页面跳转执行过程
    orcale 使用创建日期排序然后分页每次取排序后的固定条数
    JSON语法规则
    Windows idea 搜狗输入法输入中文只显示英文
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4588109.html
Copyright © 2011-2022 走看看