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

来源:互联网 发布:windows.old 编辑:程序博客网 时间:2024/06/06 08:43
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
原创粉丝点击