HDU 6034 Balala Power!(进制)

来源:互联网 发布:java 工程师 简历 编辑:程序博客网 时间:2024/05/24 15:39

KazaQ’s Socks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 438 Accepted Submission(s): 284

Problem Description
KazaQ wears socks everyday.

At the beginning, he has n pairs of socks numbered from 1 to n in his closets.

Every morning, he puts on a pair of socks which has the smallest number in the closets.

Every evening, he puts this pair of socks in the basket. If there are n−1 pairs of socks in the basket now, lazy KazaQ has to wash them. These socks will be put in the closets again in tomorrow evening.

KazaQ would like to know which pair of socks he should wear on the k-th day.

Input
The input consists of multiple test cases. (about 2000)

For each case, there is a line contains two numbers n,k (2≤n≤109,1≤k≤1018).

Output
For each test case, output “Case #x: y” in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.

Sample Input

3 7
3 6
4 9

Sample Output

Case #1: 3
Case #2: 1
Case #3: 2

Source
2017 Multi-University Training Contest - Team 1

题意:
a-z分别可变为0-25,给你n个字符串,把变成26进制的数,加起来合最大。每个字符串除了单个0,没有前置0。

开一个数组记录某字母在第几位出现了几次,并找出价值最小的字
母赋为0;然后按价值大小从25-1赋值。注意0位置。

具体看代码注释。

#include <cstdio>#include <cstring>#include <iostream>#include <map>#include <algorithm>#include <cmath>using namespace std;#define  LL long longconst LL p = 1e9+7;LL m[100000+5];const int maxn=100000+7;int vis[26];struct node{    int flag;    char num[maxn];}num[27];///计算26的0-100000次方mod(p)///用m[]数组存void init(){    m[0]=1;    LL now=1;    for(int i=1;i<=100000;i++)    {        now*=26;        m[i]=now%p;        now=now%p;    }}///排序算法  降序bool cmd(node a,node b){    for(int i=maxn-1;i>0;i--)    {        if(a.num[i]!=b.num[i])        {            return a.num[i]>b.num[i];        }        else ;    }    return a.num[0]>b.num[0];}int main(){    int n;    init();    int pp=0;    while(~scanf("%d",&n))    {        char str[maxn];        for(int i=0;i<26;i++)        {            num[i].flag=i;///            vis[i]=0;///判断            for(int j=0;j<maxn;j++) num[i].num[j]=0;        }        ///输入并处理        for(int i=1;i<=n;i++)        {            scanf("%s",str);            int l=strlen(str);///字符串的长度            if(l!=1) vis[str[0]-'a']=1;///字母str[0]不能为0;            ///遍历字符串            for(int j=0;j<l;j++)            {                int x=str[j]-'a';///将字母转化成数字 便于统计                ///2425=25*26^0+24*26^1;                int y=l-j-1;///进制转化是从后向前 将位置转化                num[x].num[y]++;///记录字母str[j]在j处出现了几次                ///进位                while(num[x].num[y]==26)//防止一直进位,用while-这里很重要,不仔细就wa了                {                    num[x].num[y++]=0;                    num[x].num[y]++;                }            }        }        sort(num,num+26,cmd);///将记录下的数据按降序排序        ///2425=25*26^0+24*26^1;        ///进制转化是从后向前        LL ans = 0;        int f=-1;        ///找到价值最小的可以为0的字母 此字母赋值为0        for(int i=25;i>=0;i--)        {            if(!vis[num[i].flag])            {                f=i;                break;            }        }        ///计算        for(int i=0;i<=25;i++)        {            if(i==f);            else if(i<f)            {                LL haha=25-i;///赋给字母i的值                for(int j=maxn;j>=0;j--)                {                    (ans+=(LL)haha*m[j]*num[i].num[j])%=p;                }            }            else            {                LL haha=25-i+1;                for(int j=maxn;j>=0;j--)                {                    (ans+=(LL)haha*m[j]*num[i].num[j])%=p;                }            }        }        printf("Case #%d: %lld\n",++pp,ans);    }}
原创粉丝点击