Topcoder SRM 320 Div1 250

题意:给两个大整数,判断哪个更大。大整数以”AB”形式给出,”A”是一个不含前导0的整数(大于0,不超过1e9),”B”是若干个(可能为空)阶乘符号(“!”)。比如:3!!=6!=720

解法:设两个大整数形式A部分分别为a,b;B部分分别有n1,n2个符号。假设n1 > n2,那么我们只需判断aA 和 b的大小即可,其中A为(n1 - n2)个阶乘符号。n1 = n2 或者 n1 < n2时候类似。但要注意有坑,当a为0或b为0时,如果可以将a或b转化为1,就转换。否则可能会有坑,比如1!!!!! = 0!!

My Code

#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<set>
#include<map>
using namespace std;
typedef long long ll;

class ExtraordinarilyLarge{
public:
    string compare(string x, string y){
        string ans1 = "x<y", ans2 = "x=y", ans3 = "x>y";
        int n1 = 0;
        while(x.back() == ‘!‘) n1++, x.pop_back();
        int n2 = 0;
        while(y.back() == ‘!‘) n2++, y.pop_back();
        ll a = stol(x), b = stol(y);

        if(a == 0 && n1 > 0) a = 1, n1--;
        if(b == 0 && n2 > 0) b = 1, n2--;

        if(n1 == n2)
        {
            if(a < b) return ans1;
            else if(a == b) return ans2;
            else return ans3;
        }
        else if(n1 > n2)
        {
            ll c = 1;
            for(int i = 0; i < n1 - n2; i++)
            {
                c = 1;
                for(int j = 1; j <= a; j++)
                {
                    c = c * j;
                    if(c > b) return ans3;
                }
                a = c;
            }
            if(a < b) return ans1;
            else if(a == b) return ans2;
            else return ans3;
        }
        else//n1 < n2
        {
            ll c = 1;
            for(int i = 0; i < n2 - n1; i++)
            {
                c = 1;
                for(int j = 1; j <= b; j++)
                {
                    c = c * j;
                    if(c > a) return ans1;
                }
                b = c;
            }
            if(a < b) return ans1;
            else if(a == b) return ans2;
            else return ans3;
        }
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 15:59:45

Topcoder SRM 320 Div1 250的相关文章

Topcoder SRM 643 Div1 250&lt;peter_pan&gt;

Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*......*pn,我们假设p0,p1,...,pn是单调不降的,那么v里存储的是下标为偶数 的N的质因数p0,p2,p4,...,p(2k).现在要求写一个程序,返回一个vector<long long>ans; ans里存储的是p0,p1,p2,...,pn. Limits Time Limit(m

Topcoder SRM 648 Div1 250

Problem 给一个长度为N的"AB"字符串S,S只含有两种字符'A' 或 'B',设pair(i,j)(0=<i<j<N)表示一对 i,j 使得S[i]='A',S[j]='B'.现给定一个K,求字符串S,使得pair(i,j)的个数恰好为K.若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [2, 50] K: [0 , N*(N-1)/2 ] Solution 若K>(N/2

topcoder srm 320 div1

problem1 link 两个数字后面都有阶乘符号,可以抵消. import java.util.*; import java.math.*; import static java.lang.Math.*; public class ExtraordinarilyLarge { public String compare(String x, String y) { boolean both=false; while(x.endsWith("!")&&y.endsWit

Topcoder SRM 345 Div1 250

题意:起初在(0,0),要到(x,y)去,每次只能横向或纵向移动.横向移动时,若所在直线y为偶数,那么只能往x轴正方向移动,若为奇数,只能往x轴反方向移动:纵向移动时,若所在直线x为偶数,那么只能往y轴正方向移动,若为奇数,只能往y轴反方向移动.问从起点到终点的最短距离是多少? x,y 范围是[-1e6, 1e6] 解法:一开始想到bfs(想到很自然),将(0, 0), (x, y), (x, 0), (0, y)这4个点分别周围9个点(包括自己)作为可达点.bfs处理出(0, 0)到(x, y

Topcoder SRM 653 Div1 250

Problem N个人坐成一排,属于同一个公司的人都坐在一起(挨在一起),但不知道谁属于哪个公司.现在问其中的某些人 他属于哪个公司,他的回答是Ai,Ai=0表示此人未被询问,否则表示他的回答.题目保证一定存在一种分组方案使得属于同一公司的人都坐在一起.现问方案是否唯一? Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N,Ai∈[1,100] Solution 设dp[i]表示从i位置开始往后分组的情况,dp[i]=0表示无法分组,dp[i]=1表示

Topcoder SRM 488 Div1 250(概率dp)

题意:有n个有聊人和m个无聊人,每次等概率任选两个人,让他们都变成无聊人,求所有人都变成无聊人的期望次数.(1≤n,m≤47). 解法:设f(i)表示存在 i 个有聊人,将所有人都变成无聊人的期望次数.显然f(0) = 0,即不需要改变.方程:f(i)=f(i?2)×C2iC2n+m+f(i?1)×i×(n+m?i)C2n+m+f(i)×C2n+m?iC2n+m+1. 答案即为f(m) 代码 #include<cstdio> #include<algorithm> #include

Topcoder SRM 564 Div1 250

题意:给一个n*m的棋盘,自己选择一个位置(x,y),放置一个马,马可以走到(x-1,y-1),(x-1,y-2),(x-1,y+1),(x-1,y+2),(x+1,y-1),(x+1,y-2),(x+1,y+1),(x+1,y+2) 八个位置,前提是不能走出棋盘.马可以永不停息地走.问马能走到的不同位置数最多是多少? 解法:如果n>m,swap(n,m):如果n=1,ans=1,如果n=2,ans=(m+1)>>1:如果n=3且m=3,ans=8:其他情况ans=n*m: 具体为啥,还

Topcoder SRM 547 Div1 250

Problem On a horizontal line, there are two vertical pillars. The distance between their bottoms is w. The height of the first pillar is an integer, chosen uniformly at random in the range 1 through x, inclusive. The height of the second pillar is an

Topcoder SRM 392 Div1 250

题意:给两个字符串A,B,A和B都含有小写英文字母,同时都额外含有且仅含有一个字符 ?,现希望将A,B中的字符 ? 分别替换成其它字符串(可以不同,可以相同,可以为空),使得A=B,且要求最终的A, B串(A=B)最短.或者输出不可能. 解法:让A串作为?号更靠前的串,如果A的?号之前的字符存在一个与B对应位置不同,则不可能,如果B的?号之后的字符存在一个与A对应位置不同,则不可能.否则一定有解,暴力找到最优解即可.这题可以用hash或extend-kmp的方法,达到复杂度O(N)级别,但实现有