Poor Hanamichi

Problem Description
Hanamichi is taking part in a programming contest, and he is assigned to solve a special problem as follow: Given a range [l, r] (including l and r), find out how many numbers in this range have the property: the sum of its odd digits is smaller than the sum of its even digits and the difference is 3.
A integer X can be represented in decimal as: \(X = A_n\times10^n + A_{n-1}\times10^{n-1} + \ldots + A_2\times10^2 + A_1\times10^1 + A_0\) The odd dights are \(A_1, A_3, A_5 \ldots\) and \(A_0, A_2, A_4 \ldots\) are even digits.
Hanamichi comes up with a solution, He notices that: \(10^{2k+1}\) mod 11 = -1 (or 10), \(10^{2k}\) mod 11 = 1, So X mod 11 = \((A_n\times10^n + A_{n-1}\times10^{n-1} + \ldots + A_2\times10^2 + A_1\times10^1 + A_0) \mod 11\) = \(A_n\times(-1)^n + A_{n-1}\times(-1)^{n-1} + \ldots + A_2 - A_1 + A_0\) = sum_of_even_digits – sum_of_odd_digits So he claimed that the answer is the number of numbers X in the range which satisfy the function: X mod 11 = 3. He calculate the answer in this way : Answer =  (r + 8) / 11 – (l – 1 + 8) / 11.
Rukaw heard of Hanamichi’s solution from you and he proved there is something wrong with Hanamichi’s solution. So he decided to change the test data so that Hanamichi’s solution can not pass any single test. And he asks you to do that for him.

Input
You are given a integer T (1 ≤ T ≤ 100), which tells how many single tests the final test data has. And for the following T lines, each line contains two integers l and r, which are the original test data. (1 ≤ l ≤ r ≤ \(10^{18}\))

Output
You are only allowed to change the value of r to a integer R which is not greater than the original r (and R ≥ l should be satisfied) and make Hanamichi’s solution fails this test data. If you can do that, output a single number each line, which is the smallest R you find. If not, just output -1 instead.

Sample Input
3
3 4
2 50
7 83

Sample Output
-1
-1
80
 
 
这题只要从m开始,找到第一个不满足的就可以。主要是10的18次方的范围有点吓人,其实真正的搜索范围没有这么大,直接模拟就可以。
 
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 0x3fffffff using namespace std; int a[20], len;
long long m, n, ad; void turn(long long m)
{
len = 0;
while (m > 0)
{
a[len++] = m%10;
m /= 10;
}
} void Add()
{
a[0]++;
int i = 0;
while (a[i] >= 10 && i < len-1)
{
a[i+1]++;
a[i] -= 10;
++i;
}
if (a[len-1] >= 10)
{
a[len-1] -= 10;
a[len++] = 1;
}
} bool Ans()
{
int p = 1, ans = 0;
for (int i = 0; i < len; ++i)
{
ans += p*a[i];
p *= -1;
}
if (ans == 3) return 1;
return 0;
} long long judge()
{
int k = 0;
long long l = n - m;
for (int i = 0; i <= l; ++i)
{
k += Ans();
if (k != ((m+i+8)/11) - ((m+7)/11))
{
return m+i;
}
Add();
}
return -1;
} int main()
{
//freopen ("test.txt", "r", stdin);
int T;
scanf ("%d", &T);
for (int times = 0; times < T; ++times)
{
scanf ("%I64d%I64d", &m, &n);
turn(m);
printf ("%I64d\n", judge());
}
return 0;
}

ACM学习历程—HDU4956 Poor Hanamichi(模拟)的更多相关文章

  1. ACM学习历程—Hihocoder 1177 顺子(模拟 && 排序 && gcd)(hihoCoder挑战赛12)

      时间限制:6000ms 单点时限:1000ms 内存限制:256MB   描述 你在赌场里玩梭哈,已经被发了4张牌,现在你想要知道发下一张牌后你得到顺子的概率是多少? 假定赌场使用的是一副牌,四种 ...

  2. ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)

    Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patie ...

  3. ACM学习历程—ZOJ3878 Convert QWERTY to Dvorak(Hash && 模拟)

    Description Edward, a poor copy typist, is a user of the Dvorak Layout. But now he has only a QWERTY ...

  4. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  5. ACM学习历程—HDU5668 Circle(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...

  6. ACM学习历程—BestCoder Round #75

    1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #inclu ...

  7. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  8. ACM学习历程—HDU5521 Meeting(图论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...

  9. ACM学习历程—HDU2476 String painter(动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...

随机推荐

  1. Struts2学习一----------Struts2的工作原理及HelloWorld简单实现

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...

  2. 10-客户端防表单重复提交和服务器端session防表单重复提交

    /****************************************************DoFormServlet********************************** ...

  3. linux SPI驱动——spi core(四)

    一: SPI核心,就是指/drivers/spi/目录下spi.c文件中提供给其他文件的函数,首先看下spi核心的初始化函数spi_init(void). 1: static int __init s ...

  4. 基于IAP和网口升级固件

    基于IAP和网口升级固件 一.      需求引入 现有嵌入式设备:基于ARM Cortex-M3处理器.带以太网通讯功能. 为减少设备维护成本节省宝贵的时间和金钱,须要设计网口升级固件功能. 本文描 ...

  5. ArrayList和Vector的区别?HashMap和HashTable的区别?StringBuilder、StringBuffer和String的区别?

    ArrayList和Vector的区别?从两个方面 1.同步性:ArrayList是线程不安全的,是非同步的:Vector是线程安全的,是同步的.(Java中线程的同步也就满足了安全性) 2.数值增长 ...

  6. python 基础 2.3 for 循环

    #/usr/bin/python #coding=utf-8 #@Time   :2017/10/16 10:05 #@Auther :liuzhenchuan #@File   :for 循环.py ...

  7. 查看远程分支的log

    1 将远程分支的commit fetch到本地 git fetch 2 查看远程分支的log git log <remote-branch>

  8. pgsql 数据类型

  9. python 创建一个实例:步骤一 编写一个构造函数

    编写一个构造函数 #在python中,person 类的第一件是就是记录关于人员的基本信息,这叫做实例对象属性,并且它们通常通过给类方法函数中的self 属性赋值来创建. #赋给实力属性第一个值得通常 ...

  10. PAT 甲级 1005. Spell It Right (20) 【字符串】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1005 思路 因为 n <= 10^100 所以 要用字符串读入 但是 100 * 100 ...