跳到正文

feng20130915

2125475

无条件互关(私信)ʕ̯•͡˔•̯᷅ʔ

在洛谷打开
资料、奖项与版本

资料

编号
2125475
抓取

普通加速:

C++
ios::sync_with_stdio(false);
cin.tie(nullptr);

快读模板:

C++
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
char *ptr;
size_t len;
void init_mmap(){
  struct stat file_info;
  fstat(STDIN_FILENO, &file_info);
  len=file_info.st_size;
  char *buf=(char*)mmap(nullptr,len,PROT_READ,MAP_PRIVATE,STDIN_FILENO,0);
  ptr=buf;
}
inline int read(){
  int sum=0,f=1;
  while(*ptr<'0'||*ptr>'9'){
  	if(*ptr=='-'){
  		f=-1;
  	}
  	ptr++;
  }
  while(*ptr>='0'&&*ptr<='9'){
  	sum=(sum<<3)+(sum<<1)+(*ptr^48);
  	ptr++;
  }
  return sum*f;
}

快写模板:

C++
#ifdef _WIN32
#define pc putchar
#else
#define pc putchar_unlocked
#endif
inline void write(int n){
	if(n<0){
		pc('-');
		n=-n;
	}
	if(n>9){
		write(n/10);
	}
	pc(n%10+'0');
}

有取余快速幂:

C++
long long poww(long long a,long long b,long long p){
	long long c=1;
	while(b!=0){
		if(b&1){
			c=c*a%p;
		}
		a=a*a%p;
		b>>=1;
	}
	return c;
}

无取余快速幂:

C++
long long poww(long long a,long long b){
	long long c=1;
	while(b!=0){
		if(b&1){
			c=c*a;
		}
		a=a*a;
		b>>=1;
	}
	return c;
}

求逆元

C++
typedef long long ll;
const ll p=1000000007;
ll poww(ll a,ll b,ll p){
	ll c=1;
	while(b!=0){
		if(b&1){
			c=c*a%p;
		}
		a=a*a%p;
		b>>=1;
	}
	return c;
}
ll inv(ll a){
	return poww(a,p-2,p);
}

组合数:

C++
long long C(long long n,long long k){
	if(k<0||k>n){
		return 0;
	}
	if(k>n-k){
		k=n-k;
	}
	long long ans=1;
	for(int i=1;i<=k;i++){
		ans=(ans*(n-i+1))/i;
	}
	return ans;
}

排列数:

C++
long long A(long long n,long long k){
	if(k<0||k>n){
		return 0;
	}
	long long ans=1;
	for(int i=1;i<=k;i++){
		ans*=(n-i+1);
	}
	return ans;
}

n进制转m进制:

C++
string n_to_m(int n,int m,string &ns){
	long long sum=0;
	for(char c:ns){
		c=toupper(c);
		if(c>='0'&&c<='9'){
			sum=sum*n+(c-'0');
		}else{
			sum=sum*n+(c-'A'+10);
		}
	}
	if(sum==0){
		return "0";
	}
	string dn="";
	while(sum!=0){
		int a=sum%m;
		if(a>=10){
			dn=char(a-10+'A')+dn;
		}else{
			dn=char(a+'0')+dn;
		}
		sum/=m;
	}
	return dn;
}

字符串转大写:

C++
string to_big(string &s){
	for(int i=0;i<s.size();i++){
		if(s[i]>='a'&&s[i]<='z'){
			s[i]-=32;
		}
	}
	return s;
}

字符串转小写:

C++
string to_small(string &s){
	for(int i=0;i<s.size();i++){
		if(s[i]>='A'&&s[i]<='Z'){
			s[i]+=32;
		}
	}
	return s;
}

字符串哈希:

C++
typedef unsigned long long ull;
const int MOD=999999937;
ull strhash(string &s){
	ull u1=0; 
	for(char c:s){
		u1=((u1*131ll)%MOD+c)%MOD;
	}
	return u1;
}

判断质数:

C++
bool zhi(int n){
	if(n<=1){
		return false;
	}
	if(n==2||n==3||n==5){
		return true;
	}
	if(n%2==0||n%3==0||n%5==0){
		return false;
	}
	for(int i=5;i*i<=n;i+=6){
		if(n%i==0||n%(i+2)==0){
			return false;
		}
	}
	return true;
}

线性筛质数:

C++
const int MAX=1e8+5;
bitset <MAX> not_p;
int cnt=0,p[MAX/10];
void aaa(int n){
	not_p[0]=not_p[1]=true;
	for(int i=2;i<=n;i++){
		if(!not_p[i]){
			p[++cnt]=i;
		}
		for(int j=1;j<=cnt&&1ll*i*p[j]<=n;j++){
			not_p[i*p[j]]=true;
			if(i%p[j]==0){
				break;
			}
		}
	}
}

整数高精度:

C++
struct BigInt{
	short now[50005]={};
	char zheng_fu='+';
	int len=0;
	BigInt(int n=0){
		zheng_fu='+';
		len=0;
		string s=to_string(n);
		if(s[0]=='-'){
			zheng_fu='-';
			s=s.substr(1);
		}
		if(s.empty()){
			len=1;
			now[0]=0;
			return;
		}
		if(!s.empty()){
			len=s.size();
			for(int i=0;i<len;i++){
				now[i]=s[len-i-1]-'0';
			}
			while(len>1&&now[len-1]==0){
				len--;
			}
		}
		if(len==1&&now[0]==0){
			zheng_fu='+';
		}
	}
	BigInt(string s){
		zheng_fu='+';
		len=0;
		if(s.empty()){
			len=1;
			now[0]=0;
			return;
		}
		if(s[0]=='-'){
			zheng_fu='-';
			s=s.substr(1);
		}
		if(!s.empty()){
			len=s.size();
			for(int i=0;i<len;i++){
				now[i]=s[len-i-1]-'0';
			}
			while(len>1&&now[len-1]==0){
				len--;
			}
		}
		if(len==1&&now[0]==0){
			zheng_fu='+';
		}
	}
	BigInt abs_jia(const BigInt &a,const BigInt &b){
		BigInt ans;
		int max_len=max(a.len,b.len);
		for(int i=0;i<max_len;i++){
			ans.now[i]+=a.now[i]+b.now[i];
			if(ans.now[i]>=10){
				ans.now[i]-=10;
				ans.now[i+1]+=1;
			}
		}
		if(ans.now[max_len]!=0){
			ans.len=max_len+1;
		}else{
			ans.len=max_len;
		}
		return ans;
	}
	int abs_da_xiao(const BigInt &a,const BigInt &b){
		if(a.len!=b.len){
			if(a.len>b.len){
				return 1;
			}else{
				return -1;
			}
		}else{
			for(int i=a.len-1;i>=0;i--){
				if(a.now[i]>b.now[i]){
					return 1;
				}
				if(a.now[i]<b.now[i]){
					return -1;
				}
			}
		}
		return 0;
	}
	BigInt abs_jian(const BigInt &a,const BigInt &b){
		BigInt ans;
		ans.zheng_fu='+';
		for(int i=0;i<a.len;i++){
			ans.now[i]+=a.now[i]-(i<b.len?b.now[i]:0);
			if(ans.now[i]<0){
				ans.now[i]+=10;
				ans.now[i+1]-=1;
			}
		}
		ans.len=a.len;
		while(ans.len>1&&ans.now[ans.len-1]==0){
			ans.len--;
		}
		return ans;
	}
	BigInt abs_cheng(const BigInt &a,const BigInt &b){
		BigInt ans;
		ans.zheng_fu='+';
		for(int i=0;i<a.len;i++){
			for(int j=0;j<b.len;j++){
				ans.now[i+j]+=a.now[i]*b.now[j];
				if(ans.now[i+j]>=10){
					ans.now[i+1+j]+=ans.now[i+j]/10;
					ans.now[i+j]%=10;
				}	
			}
		}
		for(int i=0;i<=a.len+b.len;i++){
			if(ans.now[i]>=10){
				ans.now[i+1]+=1;
				ans.now[i]-=10;
			}	
		}
		ans.len=a.len+b.len;
		while(ans.len>1&&ans.now[ans.len-1]==0){
			ans.len--;
		}
		return ans;
	}
	BigInt abs_chu(const BigInt &A,const BigInt &B){
		BigInt a=A,b=B;
		a.zheng_fu='+';
		b.zheng_fu='+';
		short ok=abs_da_xiao(a,b);
		if(ok==-1){
			return BigInt(0);
		}
		if(ok==0){
			return BigInt(1);
		}
		BigInt ans;
		ans.len=a.len-b.len+1;
		BigInt yu=a;
		for(int i=ans.len-1;i>=0;i--){
			BigInt aa;
			aa.len=b.len+i;
			memset(aa.now,0,sizeof(aa.now));
			for(int j=0;j<b.len;j++){
				aa.now[j+i]=b.now[j];
			}
			while(abs_da_xiao(yu,aa)>=0){
				yu=abs_jian(yu,aa);
				ans.now[i]++;
			}
		}
		while(ans.len>1&&ans.now[ans.len-1]==0){
			ans.len--;
		}
		return ans;
	}
};
BigInt operator+(const BigInt &a,const BigInt &b){
	BigInt ans;
	if(a.zheng_fu==b.zheng_fu){
		ans=ans.abs_jia(a,b);
		ans.zheng_fu=a.zheng_fu;
		return ans;
	}else{
		int ok=ans.abs_da_xiao(a,b);
		if(ok==0){
			ans.zheng_fu='+';
			ans=0;
			return ans;
		}
		if(ok==1){
			ans=ans.abs_jian(a,b);
			ans.zheng_fu=a.zheng_fu;
			return ans;
		}
		if(ok==-1){
			ans=ans.abs_jian(b,a);
			ans.zheng_fu=b.zheng_fu;
			return ans;
		}
	}
	return ans;
}
BigInt operator-(const BigInt &a,const BigInt &b){
	BigInt ans=b;
	if(b.zheng_fu=='+'){
		ans.zheng_fu='-';
	}else{
		ans.zheng_fu='+';
	}
	return a+ans;
}
BigInt operator*(const BigInt &a,const BigInt &b){
	BigInt ans;
	ans=ans.abs_cheng(a,b);
	if(a.zheng_fu!=b.zheng_fu){
		ans.zheng_fu='-';
	}
	if(ans.len==1&&ans.now[0]==0){
		ans.zheng_fu='+';
	}
	return ans;
}
BigInt operator/(const BigInt &a,const BigInt &b){
	BigInt aaa;
	BigInt ans=aaa.abs_chu(a,b);
	if(a.zheng_fu!=b.zheng_fu){
		ans.zheng_fu='-';
	}
	return ans;
}
BigInt operator%(const BigInt &a,const BigInt &b){
	BigInt A=a,B=b;
	A.zheng_fu='+';
	B.zheng_fu='+';
	char c=a.zheng_fu;
	BigInt shang=A.abs_chu(A,B);
	A=A-(shang*B);
	A.zheng_fu=c;
	return A;
}
ostream& operator<<(ostream &a,const BigInt &x){
	if(x.zheng_fu=='-'){
		a<<x.zheng_fu;
	}
	for(int i=x.len-1;i>=0;i--){
		a<<x.now[i];
	}
	return a;
}
istream& operator>>(istream &a,BigInt &x){
	string s;
	a>>s;
	x=BigInt(s);
	return a;
}
BigInt& operator+=(BigInt &a,const BigInt &b){
	a=a+b;
	return a;
}
BigInt& operator*=(BigInt &a,const BigInt &b){
	a=a*b;
	return a;
}
BigInt& operator-=(BigInt &a,const BigInt &b){
	a=a-b;
	return a;
}
BigInt& operator/=(BigInt &a,const BigInt &b){
	a=a/b;
	return a;
}
BigInt& operator%=(BigInt &a,const BigInt &b){
	a=a%b;
	return a;
}
bool operator==(const BigInt &a,const BigInt &b){
	if(a.zheng_fu!=b.zheng_fu){
		return 0;
	}
	if(a.len!=b.len){
		return 0;
	}
	for(int i=0;i<a.len;i++){
		if(a.now[i]!=b.now[i]){
			return 0;
		}
	}
	return 1;
};
bool operator!=(const BigInt &a,const BigInt &b){
	return !(a==b);
};
bool operator<(const BigInt &a,const BigInt &b){
	if(a.zheng_fu!=b.zheng_fu){
		if(a.zheng_fu=='+'){
			return 0;
		}else{
			return 1;
		}
	}
	if(a.len!=b.len){
		if(a.zheng_fu=='+'){
			return a.len<b.len;
		}else{
			return a.len>b.len;
		}
	}
	for(int i=a.len-1;i>=0;i--){
		if(a.now[i]!=b.now[i]){
			if(a.zheng_fu=='+'){
				return a.now[i]<b.now[i];
			}else{
				return a.now[i]>b.now[i];
			}
		}
	}
	return 0;
};
bool operator>(const BigInt &a,const BigInt &b){
	return b<a;
}
bool operator<=(const BigInt &a,const BigInt &b){
	return (a<b)||(a==b);
}
bool operator>=(const BigInt &a,const BigInt &b){
	return (a>b)||(a==b);
}
共 1 篇
  1. 题解

    题解:B4563 [山东省小学组体验营 2026] 星际密信

    该代码的功能是将字符串中的每个大写字母按给定的偏移量k进行循环移动(k为负数时向前移动)。具体步骤如下: 处理偏移量:将k对26取模,若结果为负数则加26使其变为0-25的范围,确保循环移动的正确性。 遍历字符:逐个检查字符串中的字符: 若为数字,直接输出; 若为大写字母,将其转换为0-25的数值(通过c-'A'),加上偏移量后对26取模,再转回大写字母输出。 此方法通过调整k的值和字符的数值运算,实现了字母的循环移位,同时保留数字不变。

    1 赞