大意: $101$个砝码, 重$w^0,w^1,...,w^{100}$, 求能否称出重量$m$.

w<=3时显然可以称出所有重量, 否则可以暴力双端搜索.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head int w, m;
ll a[100];
unordered_map<ll,int> f[2];
void dfs(int d, int mx, ll num, unordered_map<ll,int>& f) {
if (d>mx) ++f[num];
else {
dfs(d+1,mx,num,f);
dfs(d+1,mx,num+a[d],f);
dfs(d+1,mx,num-a[d],f);
}
} int main() {
scanf("%d%d", &w, &m);
if (w<=3) return puts("YES"),0;
ll now = 1;
REP(i,0,16) {
a[++*a] = now;
now *= w;
}
dfs(1,*a/2,0,f[0]);
dfs(*a/2+1,*a,0,f[1]);
for (auto &&t:f[0]) {
if (f[1].count(t.x+m)) return puts("YES"),0;
}
puts("NO");
}

实际上有更优做法.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head int main() {
int w, m;
scanf("%d%d", &w, &m);
while (m) {
if ((m-1)%w==0) --m;
else if ((m+1)%w==0) ++m;
else if (m%w) return puts("NO"),0;
m /= w;
}
puts("YES");
}

Vanya and Scales CodeForces - 552C (思维)的更多相关文章

  1. CodeForces 552C Vanya and Scales

    Vanya and Scales Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  2. 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/p ...

  3. 暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales

    题目传送门 /* 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码 ...

  4. codeforces C. Vanya and Scales

    C. Vanya and Scales Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w10 ...

  5. Codeforces Round #308 (Div. 2)----C. Vanya and Scales

    C. Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Vanya and Scales(思维)

    Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. Codeforces 552C Vanya and Scales(进制转换+思维)

    题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在 ...

  8. 【30.23%】【codeforces 552C】Vanya and Scales

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. Codeforces 552C Vanya and Scales(思路)

    题目大概说有101个质量w0.w1.w2.....w100的砝码,和一个质量m的物品,问能否在天平两边放物品和砝码使其平衡. 哎,怎么没想到..注意到w0.w1.w2.....w100—— 把m转化成 ...

随机推荐

  1. zookeeper系列(九)zookeeper的会话详解

    作者:leesf    掌控之中,才会成功:掌控之外,注定失败. 出处:http://www.cnblogs.com/leesf456/p/6103870.html尊重原创,大家共同学习: 一.前言 ...

  2. shell脚本编程进阶

    在linux shell中,通常我们将一些命令写在一个文件中就算是一个shell脚本了,但是如果需要执行更为复杂的逻辑判断,我们就需要使用流程控制语句来支持了.所谓流程控制既是通过使用流程控制语句对程 ...

  3. HearthBuddy DONOTDELETE.bin

    namespace Hearthbuddy{    // Token: 0x02000022 RID: 34    public class App : System.Windows.Applicat ...

  4. mongodb 的云数据库产品 mlab 的使用

    mongodb的云数据库产品mlab,新用户注册,提供500m免费的空间,对于创建测试的网站数据库来说,足够使用.虽然是服务器是在美国,但是链接稳定.下面就介绍注册和使用的流程. 浏览器中,输入网址h ...

  5. 使用 docker 快速安装 oracle 11g

    前言 我们在手动安装oracle数据库时,安装步骤纷繁复杂,耗时较长 在此介绍如何使用docker快速安装oracle 11g 一.docker 及其安装环境 操作系统: [root@centos7 ...

  6. pytorch-mnist神经网络训练

    在net.py里面构造网络,网络的结构为输入为28*28,第一层隐藏层的输出为300, 第二层输出的输出为100, 最后一层的输出层为10, net.py import torch from torc ...

  7. 如何配置git send-email相关的邮箱信息?

    关键是配置smtpserver,请参考此处

  8. 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变

    带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115   近期有 ...

  9. 加载大图片的OOM异常

    * 原因:系统分配给应用程序的堆内存 < 图片的大小* 解决方案:缩放图片显示* OOM:OutOfMemoryError * 图片的宽高 * 宽 2400 * 高 3200 * 手机屏幕的宽高 ...

  10. Activity节点

    1.android:allowTaskReparenting Android:allowTaskReparenting是一个任务调整属性,它表明当这个任务重新被送到前台时,该应用程序所定义的Activ ...