Codeforces Beta Round #64 C. Lucky Tickets
In Walrusland public transport tickets are characterized by two integers: by the number of the series and by the number of the ticket in the series. Let the series number be represented by a and the ticket number — by b, then a ticket is described by the ordered pair of numbers (a, b).
The walruses believe that a ticket is lucky if a * b = rev(a) * rev(b). The function rev(x) reverses a number written in the decimal system, at that the leading zeroes disappear. For example, rev(12343) = 34321, rev(1200) = 21.
The Public Transport Management Committee wants to release x series, each containing y tickets, so that at least w lucky tickets were released and the total number of released tickets (x * y) were minimum. The series are numbered from 1 to x inclusive. The tickets in each series are numbered from 1 to y inclusive. The Transport Committee cannot release more than maxx series and more than maxytickets in one series.
The first line contains three integers maxx, maxy, w (1 ≤ maxx, maxy ≤ 105, 1 ≤ w ≤ 107).
Print on a single line two space-separated numbers, the x and the y. If there are several possible variants, print any of them. If such xand y do not exist, print a single number - 1.
2 2 1
1 1
132 10 35
7 5
5 18 1000
-1
48 132 235
22 111
1. 计算a/rev(a)的比值,然后以这个比值做排序,然后可以快速算出满足a*b=rev(a)*rev(b)的b的值
2.然后扫描一遍即可
#include <iostream> #include<vector> #include<stdio.h> #include<algorithm> #define inf 1000000000000 using namespace std; struct node{ __int64 x,y,z; }; vector<node> b,f; __int64 a[200000]; int tmp[1000],c[200000][3],p[200000]; vector<int> g[200000]; bool cmp(node a,node b) { return ((a.x*b.y<a.y*b.x));//||(a.x*b.y==a.y*b.x&&a.x<b.x)); } int dog(int x) { int i,ret=0; for(i=1;x!=0;i++) { tmp[i]=x%10; x=x/10; } for(int j=i-1,k=1;j>=1;j--,k*=10) ret+=tmp[j]*k; return ret; } int main() { int n,mx,my,x1,x2,k1,k2,k3,k4,j,k; __int64 w,ans; while(scanf("%d%d%I64d",&mx,&my,&w)!=EOF) { b.clear(); n=max(mx,my); for(int i=1;i<=n;i++) { a[i]=dog(i); node nd; nd.x=i; nd.y=a[i]; b.push_back(nd); } sort(b.begin(),b.end(),cmp); f.push_back(b[0]); f[0].z=1; int t1=1; k3=n-1; for(int i=0;i<=n;i++) { g[i].clear(); p[i]=0; } for(int i=0;i<n;i=k2+1) { k1=i; for(j=i;j<n;j++) if (b[j].x*b[k1].y!=b[j].y*b[k1].x) break; k2=j-1; for(j=k3;j>=0;j--) if (b[i].x*b[j].x<=b[i].y*b[j].y) break; k3=j; if (k3<0) continue; if(b[i].x*b[j].x!=b[i].y*b[j].y) continue; for(k=j;k>=0;k--) if (b[j].x*b[k].y!=b[j].y*b[k].x) break; k4=k+1; for(j=k1;j<=k2;j++) for(k=k4;k<=k3;k++) g[b[j].x].push_back(b[k].x); i=k2+1; } ans=inf; int pt=my,tot=0; for(int i=1;i<=mx;i++) { for(j=0;j<g[i].size();j++) { if (g[i][j]>pt) continue; p[g[i][j]]++; tot++; } if (tot<w) continue; if (ans>(__int64)(i)*(__int64)(pt)) { ans=(__int64)(i)*(__int64)(pt); x1=i; x2=pt; } for(j=pt;j>=1;j--) { if (tot-p[j]>=w) { tot-=p[j]; if (ans>(__int64)(i)*(__int64)(j-1)) { ans=(__int64)(i)*(__int64)(j-1); x1=i; x2=j-1; } } else { pt=j; break; } } } if (ans!=inf) printf("%d %d\n",x1,x2); else printf("-1\n"); } return 0; }