Problem Statement |
|
Given positive integers x and Y, their similarity s (x, y) is defined as follows:to compute s (x, y) we count all D bet Ween 0 and 9, inclusive, such that both X and y contain the digit d when written in base (without any leading zeros). For example, S (1123, 220181) = 2 Since both numbers contain the digit 1 and both contain the digit 2. You are given the ints L and R that define a range. Find, distinct integers in this range, that has the largest similarity. Formally, return the maximum of S (A, b) over all A, b such thatL <= a < b <=R. |
Definition |
|
Class: |
Similars |
Method: |
Maxsim |
Parameters: |
int, int |
Returns: |
Int |
Method Signature: |
int Maxsim (int L, int R) |
(Be sure your method was public) |
|
Limits |
|
Time limit (s): |
2.000 |
Memory Limit (MB): |
256 |
Stack Limit (MB): |
256 |
|
Constraints |
- |
R 'll be between 2 and 100,000, inclusive. |
- |
L 'll be between 1 and R -1, inclusive. |
Examples |
0) |
|
|
|
Returns:1 |
We have S (1, ten) = 1 Since both numbers contain the digit 1. All other pairs of numbers within this range has similarity 0. |
|
|
1) |
|
|
|
Returns:2 |
There is many pairs with similarity 2, for example pairs (23,32) and (38,83). |
|
|
2) |
|
|
|
returns:0 |
Here we have only one pair (similarity, + and its) 0. |
|
|
3) |
|
|
|
4) |
|
|
|
directly to each number according to the number of 2^10 pressure into a number, a total of statistics can be
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (i,n) for (int i=n;i>=0;i--) #define MEM (a) memset (A,0,sizeof (a)); #define INF (2139062143) #define F (100000007) #define MAXN (100000) typedef long Long Ll;int f[maxn]={0},g [Maxn]={0};int p2[20]={1,2,4,8,16,32,64,128,256,512,1024};class similars{public:int maxsim (int L, int R) {int ANS=0;P2 [0]=1; for (i,10) p2[i]=p2[i-1]*2; MEM (f) Fork (i,l,r) {int p=i,t=0;while (p) {t=t|p2[p%10];p/=10;} f[t]++;} Rep (i,p2[10]) {int q=0; REP (j,10) if (i&p2[j]) q++;g[i]=q;} Rep (I,p2[10]) rep (j,p2[10]) {if (i==j && f[i]>=2) | | (I!=j && f[i] && f[j])) {Ans=max (ans,g[i&j]);}} return ans;};
TC Tco2015r1a (similars-State compression)