进制转换
十进制→N进制
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<stack>
using namespace std;
char IntToChar(int target)
{
if(target<10)
{
return target+'0';
}else
{
return target-10+'A';
}
}
void Convert(int n,int x)
{
vector<char> answer;
if(n==0)
{
answer.push_back('0');
}else
{
while(n!=0)
{
answer.push_back(IntToChar(n%x));
n/=x;
}
}
for(int i=answer.size()-1;i>=0;--i)
{
printf("%c ",answer[i]);
}
printf("\n");
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
Convert(n,16);
}
return 0;
}
M进制→十进制
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<stack>
using namespace std;
int CharToInt(char target)
{
if('0'<=target&&target<='9')
{
return target-'0';
}else
{
return target-'A'+10;
}
}
void Convert(string str,int x)
{
int number=0;
for(int i=0;i<str.size();++i)
{
number*=x;
number+=CharToInt(str[i]);
}
printf("%d\n",number);
}
int main()
{
string str;
while(cin>>str)
{
str=str.substr(2);
Convert(str,16);
}
return 0;
}
M进制→N进制
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<stack>
using namespace std;
int CharToInt(char target)
{
if('0'<=target&&target<='9')
{
return target-'0';
}else
{
return target-'A'+10;
}
}
char IntToChar(int target)
{
if(target<10)
{
return target+'0';
}else
{
return target-10+'a';
}
}
long long ConvertToTen(string str,int m)
{
long long number=0;
for(int i=0;i<str.size();++i)
{
number*=m;
number+=CharToInt(str[i]);
}
return number;
}
void ConvertToN(long long number,int n)
{
vector<char> answer;
if(number==0)
{
answer.push_back('0');
}else
{
while(number!=0)
{
answer.push_back(IntToChar(number%n));
number/=n;
}
}
for(int i=answer.size()-1;i>=0;--i)
{
printf("%c ",answer[i]);
}
printf("\n");
}
int main()
{
int n,m;
while(scanf("%d%d",&m,&n)!=EOF)
{
string str;
cin>>str;
long long number=ConvertToTen(str,m);
ConvertToN(number,n);
}
return 0;
}
GCD(最大公约数)&LCM(最小公倍数)
辗转相除法
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
using namespace std;
int GCD(int a,int b)
{
if(b==0)
{
return a;
}
else
{
return GCD(b,a%b);
}
}
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
printf("%d\n",GCD(a,b));//GCD
printf("%d\n",a*b/GCD(a,b));//LCM
}
return 0;
}
质数&质因子
#include<iostream>//判断
#include<cstdio>
#include<cmath>
using namespace std;
bool Judge(int n)
{
if(n<2)
{
return false;
}
int bound=sqrt(n);
for(int i=2;i<=bound;++i)
{
if(n%i==0)
{
return false;
}
}
return true;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(Judge(n))
{
printf("yes\n");
}else
{
printf("no\n");
}
}
return 0;
}
#include<iostream>//质数筛法
#include<cstdio>
#include<vector>
using namespace std;
const int MAXN=1e5;
vector<int> prime;
bool isPrime[MAXN];
void Initial()
{
for(int i=0;i<MAXN;++i)
{
isPrime[i]=true;
}
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i<MAXN;++i)
{
if(!isPrime[i])
{
continue;
}
prime.push_back(i);
for(int j=i*i;j<MAXN;j+=i)
{
isPrime[j]=false;
}
}
return;
}
int main()
{
Initial();
int k;
while(scanf("%d",&k)!=EOF)
{
printf("%d\n",prime[k-1]);
}
return 0;
}
#include<iostream>//质因子的个数
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int MAXN=sqrt(1e9);
vector<int> prime;
bool isPrime[MAXN];
void Initial()
{
for(int i=0;i<MAXN;++i)
{
isPrime[i]=true;
}
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i<MAXN;++i)
{
if(!isPrime[i])
{
continue;
}
prime.push_back(i);
for(int j=i*i;j<MAXN;j+=i)
{
isPrime[j]=false;
}
}
return;
}
int NumberOfPrimeFactor(int number)
{
int answer=0;
for(int i=0;i<prime.size();++i)
{
int factor=prime[i];
int current=0;
if(factor>number)
{
break;
}
while(number%factor==0)
{
current++;
number/=factor;
}
answer+=current;
}
if(number>1)
{
answer++;
}
return answer;
}
int main()
{
Initial();
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",NumberOfPrimeFactor(n));
}
return 0;
}
#include<iostream>//约数的个数
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int MAXN=sqrt(1e9);
vector<int> prime;
bool isPrime[MAXN];
void Initial()
{
for(int i=0;i<MAXN;++i)
{
isPrime[i]=true;
}
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i<MAXN;++i)
{
if(!isPrime[i])
{
continue;
}
prime.push_back(i);
for(int j=i*i;j<MAXN;j+=i)
{
isPrime[j]=false;
}
}
return;
}
int NumberOfFactor(int number)
{
vector<int> exponent;
for(int i=0;i<prime.size();++i)
{
int factor=prime[i];
int current=0;
if(factor>number)
{
break;
}
while(number%factor==0)
{
current++;
number/=factor;
}
exponent.push_back(current);
}
if(number>1)
{
exponent.push_back(1);
}
int answer=1;
for(int i=0;i<exponent.size();++i)
{
answer*=exponent[i]+1;
}
return answer;
}
int main()
{
Initial();
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
for(int i=0;i<n;++i)
{
int number;
scanf("%d",&number);
printf("%d\n",NumberOfFactor(number));
}
}
return 0;
}
快速幂
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
int QuickPower(int x,int n)
{
int mod=1000;
int answer=1;
while(n!=0)
{
if(n%2==1)
{
answer*=x;
answer%=mod;
}
n/=2;
x*=x;
x%=mod;
}
return answer;
}
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==0&&b==0)
{
break;
}
printf("%d\n",QuickPower(a,b));
}
return 0;
}
矩阵&矩阵快速幂
#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN=100;
struct Matrix
{
int row,col;
int matrix[MAXN][MAXN];
Matrix(){};
Matrix(int r,int c):row(r),col(c){};
};
Matrix Add(Matrix x,Matrix y)
{
Matrix answer=Matrix(x.row,x.col);
for(int i=0;i<answer.row;++i)
{
for(int j=0;j<answer.col;++j)
{
answer.matrix[i][j]=x.matrix[i][j]+y.matrix[i][j];
}
}
return answer;
}
Matrix Multiply(Matrix x,Matrix y)
{
Matrix answer=Matrix(x.row,y.col);
for(int i=0;i<answer.row;++i)
{
for(int j=0;j<answer.col;++j)
{
answer.matrix[i][j]=0;
for(int k=0;k<x.col;++k)
{
answer.matrix[i][j]+=x.matrix[i][k]*y.matrix[k][j];
}
}
}
return answer;
}
Matrix Transpose(Matrix x)
{
Matrix answer=Matrix(x.col,x.row);
for(int i=0;i<answer.row;++i)
{
for(int j=0;j<answer.col;++j)
{
answer.matrix[i][j]=x.matrix[j][i];
}
}
return answer;
}
Matrix QuickPower(Matrix x,int n)
{
Matrix answer=Matrix(x.row,x.col);
for(int i=0;i<answer.row;++i)
{
for(int j=0;j<answer.col;++j)
{
if(i==j)
{
answer.matrix[i][j]=1;
}else
{
answer.matrix[i][j]=0;
}
}
}
while(n!=0)
{
if(n%2==1)
{
answer=Multiply(answer,x);
}
n/=2;
x=Multiply(x,x);
}
return answer;
}
void InputMatrix(Matrix& x)
{
for(int i=0;i<x.row;++i)
{
for(int j=0;j<x.col;++j)
{
scanf("%d",&x.matrix[i][j]);
}
}
return;
}
void OutputMatrix(Matrix x)
{
int i,j;
for(i=0;i<x.row;++i)
{
for(j=0;j<x.col-1;++j)
{
printf("%d ",x.matrix[i][j]);
}
printf("%d\n",x.matrix[i][j]);
}
return;
}
矩阵相加
Matrix x(3,3);
Matrix y(3,3);
InputMatrix(x);
InputMatrix(y);
Matrix answer=Add(x,y);
OutputMatrix(answer);
矩阵相乘
Matrix x(2,3);
Matrix y(3,2);
InputMatrix(x);
InputMatrix(y);
Matrix answer=Multiply(x,y);
OutputMatrix(answer);
矩阵转置
Matrix x(3,3);
InputMatrix(x);
Matrix answer=Transpose(x);
OutputMatrix(answer);
矩阵求幂
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
Matrix x=Matrix(n,n);
InputMatrix(x);
Matrix answer=QuickPower(x,k);
OutputMatrix(answer);
}
高精度整数
套用模板即可…