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. C#学习你需要知道的---(For和Foreach)

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52577283 作者:car ...

  2. [bzoj3450]Tyvj1952Easy

    瓜皮期望真是弱成渣.. 完全不理解的感觉qwq...题面%了一发千古神犇WJMZBMR哈~ Description 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化 ...

  3. CSS3 Flexbox(伸缩盒/弹性盒模型)可视化指南

    在http://css.doyoe.com/(CSS参考手册)中,本文对应其中的伸缩盒 引入 Flexbox布局官方称为CSS Flexible Box Layout Module是一个CSS3新的布 ...

  4. 绿盟python测试实习面试

    1.简历问题 低级错误:时间写错 最近好像越来越马大哈了,总是犯低级错误. 上次的开题报告首页,这次的时间,每次都有小问题,确是大毛病 到底哪里出错了 2 RHCE证书好像没有用 面试官根本就不懂这个 ...

  5. TCP close seq问题

    测试mt_hls一条流时,发现会话的时长总是对应不上. 仔细观察发现: 注意 1.包1735 (客户端) 发送FIN 请求,seq = 2435582428 2.包1736,1737,1738 (服务 ...

  6. 用CSS伪元素制作箭头

    现在让我们开始制作箭头吧! 在开始前,你要知道如何用CSS去画一个三角形,如果还不清楚可以看看这里纯CSS画各种图形 我们用到两个CSS伪元素,before和after,它们属于行内元素,但可以用di ...

  7. 【UVA10655】 Contemplation! Algebra

    题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) ...

  8. Bit与Byte的区别

    在工作中遇到一些概念模糊的地方, 需要记住了bit意为“位”或“比特”,是计算机运算的基础: byte意为“字节”,是计算机文件大小的基本计算单位: 说到usb2.0标准接口传输速率.许多人都将“48 ...

  9. Python利器一之requests

    Python利器一之requests 一.教程涉及开发语言.脚本.框架.数据库等内容 Python + requests 通过 pip 安装: pip install requests 通过 easy ...

  10. 设计模式之模板方法模式 templateMethod

    代码实现 public abstract class BankTemplateMethod { //具体方法 public void takeNumber(){ System.out.println( ...