Poor Hanamichi

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

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=An×10n+An−1×10n−1+…+A2×102+A1×101+A0
The odd dights are A1,A3,A5… and A0,A2,A4… are even digits.

Hanamichi comes up with a solution, He notices that:
102k+1 mod 11 = -1 (or 10), 102k mod 11 = 1,
So X mod 11
= (An×10n+An−1×10n−1+…+A2×102+A1×101+A0)mod11
= An×(−1)n+An−1×(−1)n−1+…+A2−A1+A0
= 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 ≤ 1018)
 
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
 
题意:现在想得到[l,r]区间里面有多少个数字的偶数位之和比奇数位大 3,然后有人就想了个办法,然后bilibilibilibili得到一个公式来计算ans = (r+8)/11-(l+1-8)/11
现在有人怀疑他这样做是错的,然后想来验证这种做法,于是给出区间[l,r],验证这段区间是否上述公式,如果满足,输出-1,不满足,输出不满足的最小的那个数字。
题解:对 [l,r]里面的每个数进行判断,先用公式求一遍,然后再判断一下这个数,如果公式所得不等于当前暴力枚举的结果,直接break
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef long long LL; bool judge(LL x){
LL bit[];
int t = ;
while(x){
bit[t++] = x%;
x/=;
}
LL odd=,even=;
for(int i=;i<t;i+=){
odd+=bit[i];
}
for(int i=;i<t;i+=){
even+=bit[i];
}
if(odd-even==) return true;
return false;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
LL l,r;
scanf("%lld%lld",&l,&r);
LL ans = -,x=;
for(LL i=l;i<=r;i++){
LL temp = (i+)/- (l-+)/;
if(judge(i)){
x++;
}
if(x!=temp){
ans = i;
break;
}
}
printf("%lld\n",ans);
}
}

hdu 4956(思路题)的更多相关文章

  1. hdu 4908(思路题)

    BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  3. hdu 5191(思路题)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. hdu 5101(思路题)

    Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. hdu 5063(思路题-反向操作数组)

    Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. hdu 4859(思路题)

    Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  7. hdu 5400(思路题)

    Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. HDU 1173 思路题

    题目大意 有n个地点(坐标为实数)需要挖矿,让选择一个地点,使得在这个地方建造基地,到n个地点的距离和最短,输出基地的坐标. 题解+代码: 1 /* 2 把这个二维分开看(即把所有点投影到x轴上,再把 ...

  9. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

随机推荐

  1. CentOS6.7下的软件安装

    一.JDK安装及其环境变量的配置 **创建一个专门安装软件的文件夹:mkdir /root/apps **解压安装包:tar -zxvf jdk-7u45-linux-x64.tar.gz -C /r ...

  2. CMD终端关于pip报错,scrapy报错的一种处理方法

    CMD终端关于pip报错,scrapy报错的一种处理方法 如果在终端输入pip,或scrapy,报如下错误: Fatal error in launcher: Unable to create pro ...

  3. stm32-IIC读写EEPROM—时序说明

    I2C 通讯协议:(Inter-Integrated Circuit)是由Phiilps 公司开发的,由于它引脚少,硬件实现简单,可扩展性强,不需要USART.CAN  等通讯协议的外部收发设备,现在 ...

  4. Leetcode 96. 不同的二叉搜索树

    题目链接 https://leetcode.com/problems/unique-binary-search-trees/description/ 题目描述 给定一个整数 n,求以 1 ... n ...

  5. [转载]C#中使用ADO.NET连接SQL Server数据库,自动增长字段用作主键,处理事务时的基本方法

    问题描述: 假设在数据库中存在以下两张数据表: User表,存放用户的基本信息,基本结构如下所示:   类型 说明 ID_User int 自动增长字段,用作该表的主键 UserName varcha ...

  6. Eclipse主题更换方法

    1.打开Eclipse的Help->Eclipse Marketplace 2.在Find里搜索Eclipse Color Theme,点击Install按钮 3.打开Window->Pr ...

  7. js数据类型的检测总结,附面试题--封装一个函数,输入任意,输出他的类型

    一.javascript 中有几种类型的值 1.基本数据类型 : 包括 Undefined.Null.Boolean.Number.String.Symbol (ES6 新增,表示独一无二的值) 特点 ...

  8. 小知识(h5 js )

    1.如果都为NaN但是他们不相等var a=NaN;var b=NaN;a==b //flase2.javascript 是一种脚本语言,可以创建服务器端和客户端的脚本3.javascript 中有两 ...

  9. laravel5.2总结--关联关系

     参考文章 http://laravelacademy.org/post/1095.html http://laravelacademy.org/post/1174.html http://d.lar ...

  10. Locust性能测试框架学习

    1. Locust简介 Locust是使用Python语言编写实现的开源性能测试工具,简洁.轻量.高效,并发机制基于gevent协程,可以实现单机模拟生成较高的并发压力. 官网:https://loc ...