进制转换
10->N
#include <iostream>
#include <stdio.h>
#include <string>//c
#include<cstring>//memset
#include<stack>
#include<vector>
using namespace std;
char IntToChar(int target) {
if (target < 10) {
return target + '0';
} else {
return target - 10 + 'A';
}
}
//10进制转换为N进制
void ConvertToN(int n, int x) {
vector<char> answer;
if (n == 0) {
answer.push_back('0');
} else {
while (n) {
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->10
#include <iostream>
#include <stdio.h>
#include <string>//c
#include<cstring>//memset
#include<stack>
#include<vector>
using namespace std;
int CharToInt(char target) {
if ('0' <= target && target <= '9') {
return target - '0';
} else {
return target - 'A' + 10;
}
}
//m进制转换为10进制
void ConvertToTen(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 <stdio.h>
#include <string>//c
#include<cstring>//memset
#include<stack>
#include<vector>
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';
}
}
//m进制转换为10进制
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;
}
//10进制转换为N进制
void ConvertToN(long long number, int n) {
vector<char> answer;
if (number == 0) {
answer.push_back('0');
} else {
while (number) {
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);
}
}
GCD & LCM
greatest common divisor(最大公约数)
#include <iostream>
#include <stdio.h>
#include <string>//c
#include<cstring>
using namespace std;
//最小公倍数
//欧几里得 辗转相除法 gcd(a,b) = gcd(b,a mod b)
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));
}
}
lowest common multiple(最小公倍数)
LCM(a,b)=(a*b)/GCD(a,b)
#include <iostream>
#include <stdio.h>
#include <string>//c
#include<cstring>
using namespace std;
//最小公倍数
//欧几里得辗转相除法 gcd(a,b) = gcd(b,a mod b)
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", (a * b) / GCD(a, b));
}
}
质数 & 质因子
质数
素数判定
#include <iostream>
#include <stdio.h>
#include<math.h>
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");
}
}
}
Prime Number
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
const int MAXN = 1e5;
vector<int> prime;
bool isPrime[MAXN];
//质数筛法获取区间内所有的质数
void Inital()
{
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()
{
Inital();
int k;
while (scanf("%d", &k) != EOF) {
printf("%d\n", prime[k - 1]);
}
return 0;
}
质因子
质因数的个数
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
const int MAXN = 4e4;//4e4
vector<int> prime;//记录区间内质数因子的个数
bool isPrime[MAXN];
//质数筛法获取区间内所有的质数
void Inital() {
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];
if (number < factor) {
break;
}
int current = 0;//当前质数因子的个数
while (number % factor == 0) {
current++;
number /= factor;
}
answer += current;
}
//最后余数不是1的质数
if (number > 1) {
answer++;
}
return answer;
}
int main() {
Inital();
int n;
while (scanf("%d", &n) != EOF) {
printf("%d\n", NumberOfPrimeFactor(n));
}
return 0;
}
约数的个数
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
const int MAXN = 4e4;//4e4
vector<int> prime;//记录区间内质数因子的个数
bool isPrime[MAXN];
//质数筛法获取区间内所有的质数
void Inital() {
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];
if (number < factor) {
break;
}
int current = 0;//当前质数因子的个数
while (number % factor == 0) {
current++;
number /= factor;
}
exponent.push_back(current);
}
//最后余数不是1的质数
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() {
Inital();
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;
}
快速幂
人见人爱A^B
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
//高效求出x的n次幂
int QuickPower(int x, int n) {
int mod = 1000;
int answer = 1;
while (n != 0) {
//n%2==1表示在二进制最低位为1
if (n % 2 == 1) {
answer *= x;
answer %= mod;
}
// n>>1;右移一位
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 <math.h>
#include <stdio.h>
#include <vector>
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 < x.row; i++) {
for (int j = 0; j < x.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) {
for (int i = 0; i < x.row; i++) {
for (int j = 0; j < x.col; j++) {
//每行最后一个数后面没有空格
if (j == 0) {
printf("%d", x.matrix[i][j]);
} else {
printf(" %d", x.matrix[i][j]);
}
}
printf("\n");
}
return;
}
int main() {
int n, k;
while (scanf("%d%d", &n, &k) != EOF) {
Matrix x = Matrix(n, n);
InputMatrix(x);
Matrix answer = QuickPower(x, k);
OutputMatrix(answer);
}
// Matrix x(2, 3);
// Matrix y(3, 2);
// InputMatrix(x);
// InputMatrix(y);
// Matrix answer = Multiply(x, y);
// OutputMatrix(answer);
return 0;
}