gay队牛逼!

我们可以强行拆一下柿子,最终得到的值会是m^k*x+m^k*u(k)*a+m^k-1*u(k-1)*a……m^0*u(0)*a

其中u表示后面有i个m的a有多少个

答案就是k+∑u

枚举每一个k,然后贪心选择u(k),那么k越大u(k)也尽可能取大,答案才会越小

其实想过拆柿子的啊,但是有些u=0我把这些位给忽略掉啦,搞得不是很会

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
inline LL write(LL x)
{
if(x>=)write(x/);
putchar(x%+'');
} LL a,b,p,q,l,r;
LL mi[]; LL now,nt,np,psd[];
LL u[];
void pro(LL k)
{
np=;
if(k==){psd[++np]=u[],nt=;return ;} if(u[]!=)
nt=,psd[++np]=u[];
else nt=;
psd[++np]=;
for(LL i=;i<k;i++)
{
if(u[i]==)psd[np]++;
else psd[++np]=u[i],psd[++np]=;
}
if(u[k]!=)psd[++np]=u[k]; reverse(psd+,psd+np+);
nt=(nt+np+)%;
}
bool calc(LL k)
{
LL x=mi[k]*p,y=mi[k]*q; now=k;
memset(u,,sizeof(u));
if(x>=l){pro(k);return true;}
for(LL i=k;i>=;i--)
{
LL d=l-x;
u[i]=d/a/mi[i];
if(x+mi[i]*u[i]*a>=l){now+=u[i];pro(k);return true;}
else if(y+mi[i]*(u[i]+)*a<=r){now+=++u[i];pro(k);return true;}
else
{
now+=u[i];
x+=mi[i]*u[i]*a;
y+=mi[i]*u[i]*a;
}
}
return false;
} LL ans,ft,tp,num[];
bool smaller()
{
if(ans==-||ans>now)return true;
else if(ans<now)return false; if(nt<ft)return true;
else if(nt>ft)return false;
for(int i=;i<=now;i++)
if(psd[i]!=num[i])return ((i+nt)%==)^(psd[i]>num[i]);
}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int T_T=;
while(scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&p,&q,&l,&r)!=EOF)
{
if(a==&&b==&&p==&&q==&&l==&&r==)break;
printf("Case %d: ",++T_T);
if(l<=p&&q<=r){puts("empty");continue;} ans=-;mi[]=;
for(LL k=;mi[k]*q<=r;k++)
{
if(mi[k]*q-mi[k]*p+>r-l+)break;
if(calc(k))
{
if(smaller())
{ans=now,ft=nt,tp=np;memcpy(num,psd,sizeof(num));}
}
mi[k+]=mi[k]*b;
if(b==)break;
}
if(ans==-){puts("impossible");}
else
{
for(LL i=;i<tp;i++)
write(num[i]),putchar(((i+ft)%==)?'A':'M'),putchar(' ');
write(num[tp]),putchar(((tp+ft)%==)?'A':'M'),puts("");
}
} return ;
}

bzoj3957: [WF2011]To Add or to Multiply的更多相关文章

  1. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

  2. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  3. UVA 1101 To Add or to Multiply

    首先我们观察加操作和乘操作会对区间产生那些影响.加操作只会平移区间,而乘操作既能移动区间还能放大区间.因此我们不难想到,如果m>1的话乘操作是log级别的,一方面是因为区间的大小不能超过s-r, ...

  4. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  5. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  6. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  7. C++ 关键字浅谈

    这里有一个游戏:要求写一个符合C++标准的程序,包含至少十个连续而且不同的关键字.连续是指不能被标识符.运算符.标点符号分割.注意这里的“不同”要求,别想用 int main() { return s ...

  8. MongoDB 聚合操作

    在MongoDB中,有两种方式计算聚合:Pipeline 和 MapReduce.Pipeline查询速度快于MapReduce,但是MapReduce的强大之处在于能够在多台Server上并行执行复 ...

  9. 一步一步HTML5粒子编辑器

    写在前面 大家阅读此文之前,可以先看一篇MiloYip的文章:用JavaScript玩转游戏物理(一)运动学模拟与粒子系统,看完之后再看此文,更加容易理解. MiloYip使用的粒子是canvas中绘 ...

随机推荐

  1. 转载:lua和c的交互

    extern "C" { #include "lua.h" #include "lualib.h" #include "lauxl ...

  2. 虚拟机搭建--hyper-V使用教程

    http://jingyan.baidu.com/article/4e5b3e19695d9f91901e24bb.html

  3. VMware虚拟机CentOS7 - VMnet8网络配置及常见问题解决

    本文环境 物理机OS: Windows10 专业版 虚拟机平台: VMware Workstation 14 虚拟机OS: CentOS 7 配置步骤 一. 查看物理主机的网络配置信息 主要是找到可以 ...

  4. 使用Eclipse+axis2一步一步发布webservice

    1.下载axis2相关软件http://axis.apache.org/axis2/java/core/download.html 2.Java环境配置:JAVA_HOME.JRE_HONE.PATH ...

  5. 准备 KVM 实验环境

    KVM 是 OpenStack 使用最广泛的 Hypervisor,本节介绍如何搭建 KVM 实验环境 安装 KVM 上一节说了,KVM 是 2 型虚拟化,是运行在操作系统之上的,所以我们先要装一个 ...

  6. POJ 3080 多个串最长公共子序列

    求多个串最长公共子序列,字典序最小输出.枚举剪枝+kmp.比较简单,我用find直接查找16ms #include<iostream> #include<string> #in ...

  7. T13432 1.计数

    题目描述 给出m个数a[1],a[2],…,a[m] 求1~n中有多少数不是a[1],a[2],…,a[m]的倍数. 输入输出格式 输入格式: 输入文件名为count.in. 第一行,包含两个整数:n ...

  8. Spring框架 JdbcTemplate类 @Junit单元测试,可以让方法独立执行 如:@Test

    package cn.zmh.PingCe; import org.junit.Test; import org.springframework.jdbc.core.BeanPropertyRowMa ...

  9. easyui combobox模糊查询

    用easyui框架开发的攻城狮恐怕都遇到过这样一个问题,就是在新增页面combobox下拉框需要支持模糊查询,但是输入不是combobox中Data里面的值的时候,点击保存,依然是可以新增进去的,这样 ...

  10. Delphi 的内存操作函数(1): 给字符指针分配内存

    马上能想到的函数有: GetMem AllocMem ReallocMem FreeMem GetMemory ReallocMemory FreeMemory New Dispose NewStr ...