玄关,差60ms。
  • 板块学术版
  • 楼主Arthur_Douglas
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/7/7 20:33
  • 上次更新2024/7/7 23:00:43
查看原帖
玄关,差60ms。
1120702
Arthur_Douglas楼主2024/7/7 20:33
#include<bits/stdc++.h>
using namespace std;
string ans;
inline int max1(int a , int b)
{
	return a > b ? a : b;
}
int A[61] , B[61] , c[102];
string and_plus(string a , string b)
{
	for(int i = 0;i < a.length();++ i)
	A[a.length() - i - 1] = (a[i] & 15);
	for(int i = 0;i < b.length();++ i)
	B[b.length() - i - 1] = (b[i] & 15);
	for(int i = 0;i < max1(a.length() , b.length());++ i)
	{
		if(i >= b.length())
		c[i] += A[i];
		else if(i >= a.length())
		c[i] += B[i];
		else
		c[i] += A[i] + B[i];
		if(c[i] > 9)
		{
			c[i + 1] += (c[i] / 10);
			c[i] %= 10;
		}
	}
	int j = max1(a.length() , b.length());
	while(c[j] == 0 && j > 0)
	-- j;
	string C;
	for(int i = j;i >= 0;-- i)
	C += (c[i] + '0') , c[i] = 0;
	return C;
}
string multiply(string a , string b)
{
	for(int i = 0;i < a.length();++ i)
	A[a.length() - i - 1] = (a[i] & 15);
	for(int i = 0;i < b.length();++ i)
	B[b.length() - i - 1] = (b[i] & 15);
	for(int i = 0;i < b.length();++ i)
	{
		for(int j = 0;j < a.length();++ j)
		{
			c[i + j] += B[i] * A[j];
			if(c[i + j] > 9)
			{
				c[i + j + 1] += (c[i + j] / 10);
				c[i + j] %= 10;
			}
		}
	}
	int j = b.length() + a.length();
	while(c[j] == 0 && j > 0)
	-- j;
	string C;
	for(int i = j;i >= 0;-- i)
	C += (c[i] + '0') , c[i] = 0;
	return C;
}
void read(string &a)
{
	a = "";
	char c = getchar();
	while(c == ' ' || c == '\n')
	c = getchar();
	while(c != ' ' && c != '\n')
	{
		a += c;
		c = getchar();
	}
}
void read(int &n)
{
    n = 0;
    char c;
    while((c = getchar()) < 48) ; 
    do
    {
        n = (n << 3) + (n << 1) + (c ^ 48);
    }while((c = getchar()) > 47);
    return ;
}
void write(int n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    if(n > 9)
    {
        write(n / 10);
    }
    putchar(n % 10 + 48);
}
string pow(string x , int k)
{
	string df = x;
	string g = "1";
	if(!k)
	return "1";
	while(k)
	{
		if(k & 1)
		g = multiply(g , df);
		df = multiply(df , df);
		k >>= 1;
	}
	return g;
}
bool Can(int sh , int x)
{
	return sh > x;
}
signed main()
{
	int T;
	read(T);
	string a;
	int x;
	string tx , ty;
	string j_and;
	int len;
	while(T --)
	{
		ans = "0";
		a = "";
		read(a);
		read(x);
		len = a.length();
		for(short i = len - 1 , l = 0;l < len;-- i , ++ l)
		{
			tx = "";
			tx += char(((a[i] & 15) - Can(a[i] & 15 , x)) + '0');
			ty = pow("9" , l);
			j_and = multiply(tx , ty);
			ans = and_plus(ans , j_and);
		}
		ans = and_plus(ans , "1");
		for(int i = 0;i < ans.length();++ i)
		putchar(ans[i]);
		putchar('\n');
	}
	return 0;
}
//5
//13 4
//15 4
//2003 4
//2005 4
//999999 4

详见

2024/7/7 20:33
加载中...