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. Android推断是否有sd卡

    推断手机上是否有SD卡存在.作为经常用法,写到工具类里,用时直接调用.代码例如以下: public static boolean hasSdcard(){ String state = Environ ...

  2. java 中的CountDownLatch

    直接使用thread可以使用thread和wait notify 实现顺序执行 线程池中可以使用CountDownLatch 进行顺序执行 package com.test; import java. ...

  3. CentOS下安装python3.x版本

    现在python都到了3.x版本,但是centos中自带的python仍然是2.7版本的,所以想把python换成3.x版本的. 但是这个地方有个坑,你要是直接编译安装了python3.x之后,估计你 ...

  4. Windows+VS+SVN实现版本控制

    Subversion已经是一个热门话题,下面介绍一下Windows下Subversion和TortoiseSVN构建SVN版本控制 问题. 首先看一些基础知识: Subversion是架设一个SVN ...

  5. win本地配置docker环境

    先上官网链接:https://docs.docker.com/get-started/part2/#introduction 优质入门教程:http://www.docker.org.cn/book/ ...

  6. mongodb 集群部署--分片服务器搭建

    部署分片服务器 1.分片 为了突破单点数据库服务器的I/O能力限制,对数据库存储进行水平扩展,严格地说,每一个服务器或者实例或者复制集就是一个分片. 2.优势 提供类似现行增·长架构 提高数据可用性 ...

  7. 公网yum 源地址

    1. centos5.*  公网yum 源地址 [root@web ~]# cd /etc/yum.repos.d/[root@web yum.repos.d]# wget -O /etc/yum.r ...

  8. JS学习总结之操作文档对象模型

    操作文档对象模型 DOM 结构树 文档对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可拓展置标语言的标准编程接口.它是一种与平台和语言无关的应用程序接口(A ...

  9. windowsphone8.1学习笔记之应用数据(一)

    数据存储分为两种:云存储和应用数据(即本地存储),wp中的应用数据分为两种,一种是应用设置:一种是应用文件.wp的数据相关都是通过ApplicationData来实现,一个程序只有数据存储区. 先说应 ...

  10. 九度OJ 1068:球的半径和体积 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5385 解决:1949 题目描述: 输入球的中心点和球上某一点的坐标,计算球的半径和体积 输入: 球的中心点和球上某一点的坐标,以如下形式输 ...