Codeforces Beta Round #64 C. Lucky Tickets

本文探讨了在Walrusland公共运输系统中如何通过优化系列数和每系列的票数来确保至少发行一定数量的幸运船票,同时最小化总的发行票数。详细介绍了计算方法及实现步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Lucky Tickets
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains three integers maxxmaxyw (1 ≤ maxx, maxy ≤ 105, 1 ≤ w ≤ 107).

Output

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.

Examples
input
2 2 1
output
1 1
input
132 10 35
output
7 5
input
5 18 1000
output
-1
input
48 132 235
output
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;
}

  

转载于:https://www.cnblogs.com/oi111/p/5260177.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值