已经菜到不行了 PAT 1010. Radix (25)
https://www.patest.cn/contests/pat-a-practise/1010
题目大意:
输入四个数字,a,b,c,d。
a和b是两个数字,c=1表示是第一个数字,c=2表示是第二个数字,d表示该数字是几进制
问:a和b是否能在某一进制下相等
思路:
傻逼二分...
一年没写过了,细节思考竟然差了这么多,尴尬了...
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
LL val;
char ch1[maxn], ch2[maxn]; LL get_int(char ch){
if (ch >= '' && ch <= '') return 1LL * (ch - '');
return 1LL * (ch - 'a' + );
}
//c 1100 1 26
bool flag;
LL cal(LL x, int n){
LL ans = ; while (n){
if (n & ) ans *= x;
x = x * x;
if (x < || ans < ) flag = false;
n /= ;
}
return ans;
} LL get_val(vector<LL> ve, int radix){
LL sum = ;
int cnt = ;
for (int i = ve.size() - ; i >= ; i--){
sum = sum + ve[i] * cal(1LL * radix, cnt);
cnt++;
}
if (sum < ) flag = false;
return sum;
} bool solve(vector<LL> b){
LL lb = , rb = val + ;
for (int i = ; i < b.size(); i++)
lb = max(lb, b[i] + );
while (lb < rb){
LL mid = (lb + rb) / ;
flag = true;
LL tmp = get_val(b, mid);
if (!flag) {
rb = mid - ; continue;
}
if (val > tmp) lb = mid + ;
if (val < tmp) rb = mid - ;
if (val == tmp) rb = mid;
}
if (get_val(b, lb) == val) {
printf("%lld\n", lb);
return true;
}
return false;
} int main(){
scanf("%s%s", ch1, ch2);
int ty, change;
scanf("%d%d", &ty, &change);
vector<LL> a, b;
if (ty == ){
for (int i = ; ch1[i] != '\0'; i++)
a.push_back(get_int(ch1[i]));
for (int i = ; ch2[i] != '\0'; i++)
b.push_back(get_int(ch2[i]));
}
else {
for (int i = ; ch2[i] != '\0'; i++)
a.push_back(get_int(ch2[i]));
for (int i = ; ch1[i] != '\0'; i++)
b.push_back(get_int(ch1[i]));
}
val = get_val(a, change);
if(!solve(b)) puts("Impossible");
return ;
}
已经菜到不行了 PAT 1010. Radix (25)的更多相关文章
- PAT 1010 Radix (25分) radix取值无限制,二分法提高效率
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT 解题报告 1010. Radix (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- PAT 1010 Radix(X)
1010 Radix (25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- 1010. Radix (25) pat
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- PAT Advanced 1010 Radix(25) [⼆分法]
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- PAT甲题题解-1010. Radix (25)-二分搜索
题意:给出n1和n2,以及其中一个数的进制,问另一个数是多少进制的情况下,才会是两个数相等.不存在的话,则输出Impossible 这题思路很简单,但是要考虑的比较多,在简单题里面算是比较好的. 有两 ...
随机推荐
- Python对list去重
Python对list去重 方法一 新建新的列表,利用not in命令去重.这种方法看起来不够简便,但是保留了原列表中的顺序.代码如下: list1 = [1,2,3,4,1,1,2,5,4,3] l ...
- NABCD(网上投票系统)
网上投票系统 N(need) 投票这件事情,在所有事情上都可能用得到,在互联网的影响下,投票的范围变得越来越广,比如在商业的里,往往要做市场分析,那么在互联网这个大的前提下,用网上投票系统来获取用户的 ...
- poi读取Excel文件和图片
首先得说一下,Excel文件是有03版和07版的区别的,也就是.xls和.xlsx,这两个文件需要分开读取. 其它的废话就不说了,直接贴代码: package util; import java.io ...
- CSS和JS引用图片(资源)的路径问题
做项目时遇到了这个问题,特此写个笔记记一下
- 栈java实现
这几天,过得挺充实的,每天都在不停的上课,早上很早就起来去跑步,晚上到图书馆看书.一边紧张的学习,一边在默默的备战软考.最近还接手了一个公司官网的建设.这是我在川信最后的一个完整学期了,每件事我都要认 ...
- hdu 3307 简单的指数循环节
#include<stdio.h>#include<string.h>#include<algorithm>#define LL __int64using name ...
- BZOJ 3210: 花神的浇花集会
3210: 花神的浇花集会 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 577 Solved: 299[Submit][Status][Discus ...
- BZOJ 1014 火星人 | 平衡树维护哈希
BZOJ 1014 火星人 题意 有一个字符串,三中操作:在某位置后面插入一个字符.修改某位置的字符.询问两个后缀的最长公共前缀. 题解 看到网上的dalao们都说这道题是平衡树,我就很懵x--平衡树 ...
- 洛谷 P1306 斐波那契公约数 解题报告
P1306 斐波那契公约数 题意:求\(Fibonacci\)数列第\(n\)项和第\(m\)项的最大公约数的最后8位. 数据范围:\(1<=n,m<=10^9\) 一些很有趣的性质 引理 ...
- MVC4.0中cshtml中怎么解析html编码
http://bbs.csdn.net/topics/391060108?page=1 问题描述: 数据库中存储带有格式的文本,如 <span style="color:#333333 ...