#include<iostream>#include<algorithm>usingnamespace std;typedeflonglong LL;//这里就得设置成 long longintget(char c){if(c <='9')return c -'0';return c -'a'+10;}
LL calc(string n, LL r)//计算给出的数的进制的10进制{
LL res =0;for(auto c : n){//注意这里用double类型,不用就是扣4分if((double)res * r +get(c)>1e16)return1e18;
res = res * r +get(c);}return res;}//接下来看下数字的细节intmain(){
string n1, n2;
cin >> n1 >> n2;int tag, radix;
cin >> tag >> radix;if(tag ==2)swap(n1, n2);
LL target =calc(n1, radix);//计算目标值
LL l =0, r =target;//l r现在是左右 边界//本题中l是2 r为6for(auto c : n2) l =max(l,(LL)get(c)+1);//去找到n2的 最小的进制数,比如110的最小进制数就是2,其实找到的是1,然而是2进制;比如ab 是1011,则找到的值是11,所以进制数为12while(l < r)//2分查找法{//>>1表示除以2
LL mid = l + r >>1;//计算他的一个中位点if(calc(n2, mid)>= target) r = mid;else l = mid +1;}if(calc(n2, l)!= target)puts("Impossible");else cout << l << endl;return0;}
A1015
#include<iostream>usingnamespace std;typedeflonglong LL;//注意这是long long类型boolis_prime(int n)//判断素数的函数{if(n ==1)returnfalse;for(int i =2; i * i <= n; i ++)if(n % i ==0)returnfalse;returntrue;}boolcheck(int n,int d){if(!is_prime(n))returnfalse;//首先如果n不是素数,直接返回false
LL r =0;while(n){
r = r * d + n % d;//首先这是用了秦九昭算法,算d进制的数,n%d算的是d进制的最后一位,再算翻转的时候,算的就是第1位
n /= d;}returnis_prime(r);}intmain(){int n, d;while(cin >> n >> d, n >=1)//n是负数就不行{if(check(n, d))puts("Yes");elseputs("No");}return0;}