[BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)
Poor Hanamichi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 743 Accepted Submission(s): 275
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.
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)
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.
3
3 4
2 50
7 83
-1
-1
80
解题思路:
题意为:存在一个问题求一个区间([l,r])内有多少数满足(偶数位的和-奇数位的和==3)。
Hanamichi提出了它的解法 Answer = (r + 8) / 11 – (l – 1 + 8) / 11.我们的任务就是找到一个最小的右端点,使得该区间内满足条件的数不满足Hanamichi的解法。
一開始读完题意想的是数位Dp,但后来用暴力搞了.....
代码:
#include <iostream>
using namespace std; long long cal(long long l,long long r)//常规方法求区间内满足条件的数有多少个
{
long long result=0;
for(;l<=r;l++)
{
long long temp=l;
int mul=1;
int re=0;
while(temp)
{
re+=mul*(temp%10);
temp/=10;
mul=(-mul);
}
if(re==3)
result++;
}
return result;
} long long re(long long l,long long r)
{
return (r+8)/11-(l-1+8)/11;
} int main()
{
long long l,r,R;
int t;
cin>>t;
while(t--)
{
cin>>l>>r;
for(R=l;R<=r;R++)//从小到大,输出最小的那个
{
long long result=re(l,R);
if(cal(l,R)!=result)
{
cout<<R<<endl;
break;
}
}
if(R>r)
cout<<-1<<endl;
}
return 0;
}
[BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)的更多相关文章
- hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Ot ...
- Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...
- [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
- 【HDOJ】4956 Poor Hanamichi
基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. #include <cstdio> int f(__int64 x) { int i, sum; i = ...
- BestCoder Round #60/HDU 5505 暴力数学
GT and numbers 问题描述 给出两个数NN和MM. NN每次可以乘上一个自己的因数变成新的NN. 求最初的NN到MM至少需要几步. 如果永远也到不了输出-1−1. 输入描述 第一行读入一个 ...
- BestCoder Round #92 (hdu 6015 6016)
比赛链接 A题主要是map的使用,比赛的时候问了下队友,下次要记住了 #include<bits/stdc++.h> using namespace std; typedef long l ...
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
随机推荐
- hdoj1160 FatMouse's Speed 动态规划
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- JS中split用法和数组中元素的删除
JS中split用法 <script language="javascript"> function spli(){ datastr="2,2,3,5,6,6 ...
- java web 中 读取windows图标并显示
java web中读取windows对应文件名的 系统图标 ....显示 1.获取系统图标工具类 package utils; import java.awt.Graphics; import j ...
- c++重载>>和<<
在重载输出输入运算符的时候,只能采用全局函数的方式(因为我们不能在ostream和istream类中编写成员函数),这里才是友元函数真正的应用场景.对于输出运算符,主要负责打印对象的内容而非控制格式, ...
- druid使用
https://blog.csdn.net/weinichendian/article/details/78503469
- VBA学习笔记(7)--vba的数组函数
说明(2017.3.25): 1. split(str,"-")和join(arr,",")函数,用法跟其他语言差不多. 2. filter函数,filter( ...
- 【WPF】设置ListBox容器Item的流式布局
需求:像下图那样显示把一组内容装入ListBox中显示.要求用WrapPanel横向布局,顺序如图中的数字. 问题:ListBox默认的布局是从上往下单列的,所以需要设置布局. <ListBox ...
- 各种不同的mq
目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常重量 ...
- java中的访问控制符
首先来一张图: 对于一个类而言访问控制符只有一个public和默认无修饰符.其他的几个访问修饰符对于变量和方法都可以使用. 下面介绍具体的使用. 1. 公有访问控制符(public) Java的类是通 ...
- u-boot 2016.05 添加自己的board 以及config.h
拿到一个uboot 后,我都想添加一个属于自己的board文件以及include/configs/*.h 文件. 如何添加这个些文件,今天来记录一下. 复制一份你所参考的板级文件,比如说board/v ...