[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 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
随机推荐
- centos 7 禁用笔记本触摸板设置
安装 xorg-x11-apps 包: yum install xorg-x11-apps. 终端输入 xinput list 找到 PS/2 记录后面的ID xinput list ⎡ Virtua ...
- 你应该知道的Virtual Studio
最近,在网上看到一篇关于VS2008的一些提示,可以提高开发效率,我把它翻译过来,当然里面也有很多自己的想法,分享一下,大家可以择有用的提示而用之. 参考:每个开发者都应该知道的提示和诀窍 提示一:拷 ...
- 在eclipse中执行sql
只要你配置好了你的database(在Data Source Explorer中,可以通过window->show view打开) 写好你的sql script,然后配置好profile 右键, ...
- LeetCode: Wildcard Matching 解题报告
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...
- RTX——第16章 消息邮箱
以下内容转载自安富莱电子: http://forum.armfly.com/forum.php 前面几个章节主要给大家讲解了任务间的同步和资源共享机制,本章节为大家讲解任务间的通信机制消息邮箱,RTX ...
- java有序的hashmap
使用LinkedHashmap可以构建一个有序的map 引用:http://wiki.jikexueyuan.com/project/java-collection/linkedhashmap.htm ...
- 【C#/WPF】TextBlock/TextBox/Label编辑文字的问题
标题有点描述不清,就当是为了方便自己用时易于搜索到. 总之需求是:显示用户信息(文字)时,允许用户编辑自己的信息. 效果图如下: 点击[编辑]按钮前: 点击[编辑]按钮后,允许编辑: 别吐槽为甚性别还 ...
- NYOJ-451-光棍节的快乐
光棍节的快乐 时间限制:1000 ms | 内存限制:65535 KB 难度: 描写叙述 光棍们,今天是光棍节.聪明的NS想到了一个活动来丰富这个光棍节. 规则例如以下: 每一个光棍在一个纸条上写 ...
- Windoows窗口程序二
WNDCLASS属性style取值: CS_GLOBALCLASS--应用程序全局窗口类 CS_BYTEALIGNCLIENT--窗口客户区的水平位置8倍数对齐 CS_BYTEALIGNWINDOW- ...
- 关于Cocos2d-x程序运行时候提示关闭程序的框框的解决方法
1.这个情况是资源没有被加载的表现 如果AppDelegate.cpp里面没有文件索引的语句 FileUtils::getInstance()->addSearchPath("res& ...