Codeforces 484 (#276 Div 1) B Maximum Value screening method

Source: Internet
Author: User

Question: Give you an array and ask you the maximum value of ai % aj in 1 <= I, j <= n ai> aj

Solution: Find a multiple of each number by screening, and find the tree that exists in the array closest to this multiple on the left (which can be preprocessed)

Solution code:

1 // Author: darkdream 2 // Created Time: thursday, November 06, 2014, 10 seconds, 3 4 # include <vector> 5 # include <list> 6 # include <map> 7 # include <set> 8 # include <deque> 9 # include <stack> 10 # include <bitset> 11 # include <algorithm> 12 # include <functional> 13 # include <numeric> 14 # include <utility> 15 # include <sstream> 16 # include <iostream> 17 # include <iomanip> 18 # include <cstdio> 19 # include <cmath> 20 # include <cstdlib> 21 # in Clude <cstring> 22 # include <ctime> 23 # define LL long long24 25 using namespace std; 26 int main () {27 int n; 28 LL l, r; 29 scanf ("% d", & n); 30 for (int I = 1; I <= n; I ++) 31 {32 LL ans = 0; 33 scanf ("% I64d % I64d", & l, & r); 34 for (int I = 62; I> = 0; I --) 35 {36 if (r & (1ll <I ))! = 0) 37 {38 if (l & (1ll <I ))! = 0) 39 {40 ans + = (1ll <I); 41 r & = (~ (1ll <I); 42 l & = (~ (1ll <I); 43} else {44 if (1ll <(I + 1)-1 <= r) 45 {46 ans + = (1ll <(I + 1)-1; 47} 48 else ans + = (1ll <I)-1; 49 break; 50} 51} 52 53} 54 printf ("% I64d \ n", ans); 55} 56 return 0; 57}
View Code

 

Codeforces 484 (#276 Div 1) B Maximum Value screening method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.