Alexandra and A*B Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 802    Accepted Submission(s): 211

Problem Description
Alexandra
has a little brother. He is new to programming. One day he is solving
the following problem: Given two positive integers A and B, output A*B.
This
problem is even easier than the last one. Alexandra can't wait to give
him another task: Given a positive integer A and a string S(S only
contains numbers.), find the minimum positive integer B, such that S is a
substring of T, where T is the decimal notation of A*B.
See the sample for better understanding.
Note: S can contain leading zeros, but T can't.
 
Input
There are multiple test cases (no more than 500). Each case contains a positive integer A and a string S.
A≤10,000,1≤|S|≤8.
 
Output
For each case, output the required B. It is guaranteed that such B always exists.
To C++ programmers: if you want to output 64-bit integers, please use "%I64d" specifier or cout.
 
Sample Input
6 8
96 19
2 0086
1 1
 
Sample Output
3
2
5043
1
 
题意:给出一个数字a,以及一个串s(lens<=8)找到一个最小的数字 b ,使得 s 是 a*b = t 的一个连续子序列.
题解:非常巧妙的题目。假设t = xsy , 那么我们可以写出表达式 t = (x*10^lens+s)*10^len+y ,又因为 t%a == 0 所以对于最小的 t 式子的每一部分,都可以对 a 取模,
所以我们可以知道 x<a ,因为如果 x >= a,我们可以通过取模操作让 x 变小, 然后如果s[0]==0,那么x的下限就是1,否则x的下限为 0,然后对于 10^len 我们也可以通过同样的道理知道 10^len<= a <= 10^4 ,然后我们化出:
设 k = (x*10^lens+s)*10^len
所以 (k+y)%a=0
y = (-k%a+a)%a (y<10^len)
所以通过枚举 x,10^len 得到最终的答案。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL; int main()
{
LL a;
char s[];
while(scanf("%lld%s",&a,s)!=EOF){
int len = strlen(s);
if(strcmp(s,"")==){
printf("0\n");
continue;
}
LL ls = ,mid=;
for(int i=;i<len;i++) ls*=;
for(int i=;i<len;i++){
mid = mid*+s[i]-'';
}
LL ans = -;
for(LL i=;i<=;i*=){
for(LL j=(s[]=='');j<a;j++){
LL t = (j*ls+mid)*i;
LL y = (a-t%a)%a;
if(y>=i) continue;
if(ans<) ans = t+y;
else ans = min(t+y,ans);
}
}
printf("%I64d\n",ans/a);
}
return ;
}
 

hdu 5109(构造数+对取模的理解程度)的更多相关文章

  1. hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值

    pog loves szh II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. HDU 4704 Sum 超大数幂取模

    很容易得出答案就是2^(n-1) 但是N暴大,所以不可以直接用幂取模,因为除法操作至少O(len)了,总时间会达到O(len*log(N)) 显然爆的一塌糊涂 套用FZU1759的模板+顺手写一个大数 ...

  3. W - Doom HDU - 5239 线段树 找取模的规律+求一个很大的数的平方对一个数取模的写法 特别的模数==2^63-2^31

    这个题目一开始感觉还是有点难的,这个模数这么大,根本就不知道怎么写,然后去搜了题解,知道了怎么去求当x很大的时候x的平方对一个数取模怎么样不会爆掉. 然后还顺便发现了一个规律就是当一个数更新一定次数之 ...

  4. HDU 1212 大整数的取模运算

    因为这里是MOD最大为100000 所以我将字符串看作5个一组,并记录后面跟了多少个100000 每次取5个数根据其数据进行取模更新 注意过程中 100000*100000会超int #include ...

  5. C#取模的理解:为什么当a<b,a%b=a?

    一,取模a%b 1,如果a>b,例如10%7=3,这是什么原因呢?可以根据下面的理解 10 =7*1+3,则模就是3 2,如果a<b,例如7%10 = 7,这时怎么得到的呢?根据下面来理解 ...

  6. HDU 4869 (递推 组合数取模)

    Problem Turn the pokers (HDU 4869) 题目大意 有m张牌,全为正面朝上.进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数. 解题分析 记正面朝上为1,朝 ...

  7. ACM-较大的数乘法取模技巧*

    比如模数是1e15这种,相乘的时候爆LL了,但是又不想用大数,咋办呢? long long ksc(long long a, long long b, long long mod){ ; while( ...

  8. POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】

    一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his ...

  9. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

    D. The Child and Sequence   At the children's day, the child came to Picks's house, and messed his h ...

随机推荐

  1. 打开Vim/Vi代码高亮

    由于新装Vim/Vi 默认是没有打开代码高亮配置的,就看到有朋友一次次到网上去找各种配置.其实Vim默认带来配置文件的样本的,只需拷贝过来就可使用. 在用户根目录(~)中新建vim的配置文件 .vim ...

  2. 【Random Forest】林轩田机器学习技法

    总体来说,林对于random forest的讲解主要是算法概况上的:某种程度上说,更注重insights. 林分别列举了Bagging和Decision Tree的各自特点: Random Fores ...

  3. 嗯,ACM按照这个一步一步来。

        转一个搞ACM需要的掌握的算法.   要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来. 适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什么就眼红, 发挥 ...

  4. 恢复误删除表黑科技之relay log大法(续)

      Preface       I've stuck twice in my previous experiments in backing up dropped tables.I am still ...

  5. Java基础-3类和对象声明与创建

    一).在1和2中有粗略介绍过类和对象的概念,在这里简单回顾一下: 对象与类:一个实际或者虚拟的物体,这个物体既是我们的对象,这个物体呢又是属于一个分类,如动物类,人类 二).创建对象: 在创建对象的时 ...

  6. 在Linux下安装ArcGIS10.2

    最近由于工作需要,沉迷可视化无法自拔,一直在研究基于GIS的地图可视化,自己在本机windows搭建了一个ArcGIS服务器,用Tableau和R调用WMS服务成功,不愧是GIS元老级应用,效果超赞. ...

  7. 小红帽安装centos的yum的一些坑!

    [root@localhost ~]# lsanaconda-ks.cfg yum-3.4.3-158.el7.centos.noarch.rpm yum-updateonboot-1.1.31-45 ...

  8. Pacemaker、corosync

    pacemaker详细介绍: http://blog.51cto.com/freeloda/1274533 corosync详细介绍: http://blog.51cto.com/freeloda/1 ...

  9. VMware下Linux配置局域网和外网访问

    我想尝试的是利用本机的ip+port来访问虚拟机上的web服务器,因为这样的话,我就能够将我的web服务器部署成为一个能让外网访问的服务器了,首先说下我的环境: 主机:系统win7,ip地址172.1 ...

  10. c++知识点总结--静态与动态联编

    静态联编是指在编译阶段就将函数实现和函数调用关联起来,因此静态联编也叫早绑定,在编译阶段就必须了解所有的函数或模块执行所需要检测的信息,它对函数的选择是基于指向对象的指针(或者引用)的类型   动态联 ...