Round #5 1001 Poor Hanamichi(原来这么简单啊)

10  2k+1  10  2k+1  10 的奇数次mod11 为-1 或10
 10 的偶数次mod11 为1
这里没有错,然后 
So X mod 11
= (A n ×10 n +A n1 ×10 n1 ++A 2 ×10 2 +A 1 ×10 1 +A 0 )mod11 
这句也没错
到这里 先看 一个式子
(a*b)mod c= ((a%c)(b%c))%c
= A n ×(1) n +A n1 ×(1) n1 ++A 2 A 1 +A 0  
再看这里,是不是正确的应该是
=( A n ×(1) n +A n1 ×(1) n1 ++A 2 A 1 +A 0   )%11 啊,这才是对的
想一下,原来的式子中,你差值为-8,3,14,25 算出来后mod11  不都是3吗嘛,而主人公则把这些都归为差值为3的了,也就是说,它的
答案多包括了差值为。。。-8,,14,25.。。。的,那么我们从l开始暴力搜索,如果遇到mod11 为3 但 差值不恰好为3的,这个不是我们要的答案,但是主人公包括了
,因此,到这个值为止,就是我们要找的最小的错误的数字了,恰好比正确答案多了1

#pragma warning(disable:4996)
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<set>
#include<map>
#include<string>
#include<stack>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define LL long long 
bool ok(LL x){
 int e, o;
 e = o = 0;
 int now = 0;
 while (x > 0){
  if (now == 0) o += x % 10;
  else e += x % 10;
  now = 1 - now;
  x /= 10;
 }
 o = o - e;
 if (o == 3) return false;
 while (o < 0) o += 11;//注意这里负数要先加11到正数为止,不然负数mod11 还是负数的
 if (o % 11 == 3) return true;
 return false;
}
LL go(LL l, LL r){
 for (LL i = l; i <= r; i++){
  if (ok(i)) return i;//如果遇到一个值,差值为。。。-8,14,25.。。且mod 11 为3的,直接return 
 }
 return -1;//没有找到,输出-1
}
int main()
{
 //freopen("aaa.txt", "r", stdin);
 //freopen("bbb.txt","w",stdout);
 int T;
 LL l, r;
 cin >> T;
 while (T--){
  cin >> l >> r;
  LL ans = go(l,r);
  cout << ans << endl;
 }
 //while (1);
 return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值