欧拉计划 21~30

21 亲和数
#include <bits/stdc++.h>
using namespace std;

int f(int x) {
	int res = 1, t = sqrt(x);
	for (int i = 2; i <= t; i ++) {
		if (x % i == 0) {
			res += i + x / i;
		}
	}
	if (t * t == x) res -= t;
	return res;
}

int main() {
	int ans = 0;
	for (int i = 1; i <= 10000; i ++) {
		int j = f(i);
		
		if (i < j && i == f(j)) {
			ans += i + j;
		}
	}
	cout << ans;

	return 0;
}
#include <bits/stdc++.h>
using namespace std;

int a[10009];

int main() {
	
	for (int i = 1; i <= 10000; i ++) {
		for (int j = 2 * i; j <= 10000; j += i) {
			a[j] += i;
		}
	}
	
	int ans = 0;
	for (int i = 1; i <= 10000; i ++) {
		int j = a[i];
		
		if (i < j && j <= 10000 && i == a[j]) {
			ans += i + j;
		}
	}
	cout << ans;

	return 0;
}
名字打分
#include <bits/stdc++.h>
using namespace std;

string s, a[6000];
int n;

int main() {
	while (cin >> s) {
		a[++ n] = s;
	}	
	sort(a + 1, a + 1 + n);
	
	int ans = 0;
	for (int i = 1; i <= n; i ++) {
		int v = 0;
		for (int j = 0; j < a[i].size(); j ++) {
			v += a[i][j] - 'A' + 1;
		}
		ans += i * v;
	}
	cout << ans;
   
    return 0;
}
所有不能写成两个盈数之和的正整数之和
#include <bits/stdc++.h>
using namespace std;

int a[30009];

int main() {
	int m = 28123;
	for (int i = 1; i <= m; i ++) {
		for (int j = 2 * i; j <= m; j += i) {
			a[j] += i;
		}
	}
	//a[i] > i为盈数 
	
	int ans = 0;
	for (int i = 1; i <= m; i ++) {
		bool flag = false;
		for (int j = 1; j <= i / 2; j ++) {
			int k = i - j;		//i = j + k
			
			if (a[j] > j && a[k] > k) {
				flag = true;
				break;
			}			
		}
		
		if (!flag) ans += i;
	}
	cout << ans;

	return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int N = 30000;
int a[N], b[N], n;
bool h[N];

int main() {
	int m = 28123;
	for (int i = 1; i <= m; i ++) {
		if (a[i] > i) b[++ n] = i;				//a[i] > i为盈数 
		
		for (int j = 2 * i; j <= m; j += i) {
			a[j] += i;
		}		
	}
	
	for (int i = 1; i <= n; i ++) {
		for (int j = i; j <= n; j ++) {
			int t = b[i] + b[j];
			if (t > m) break;
			h[t] = true;
		}
	}
	
	int ans = 0;
	for (int i = 1; i <= m; i ++) {
		if (!h[i]) {
			ans += i;
		}
	}
	cout << ans;

	return 0;
}
字典序排列
#include <bits/stdc++.h>
using namespace std;

int a[10];

int main() {
	for (int i = 0; i < 10; i ++) a[i] = i;
	
	for (int i = 1; i < 1000000; i ++) {
		next_permutation(a, a + 10);
	}
	
	for (int i = 0; i < 10; i ++) cout << a[i];

	return 0;
}
#include <bits/stdc++.h>
using namespace std;

vector <int> a, v;

int main() {
	int n = 999999;
	for (int i = 0; i < 10; i ++) {
		if (i == 0) a.push_back(1);
		else a.push_back(a.back() * i);
	
		v.push_back(i);
	}

	for (int i = 9; i >= 0; i --) {
		int x = n / a[i];
		n %= a[i];
		 
		cout << v[x];
		v.erase(v.begin() + x);
	}

	return 0;
}
1000位斐波那契数
#include <bits/stdc++.h>
using namespace std;

vector <int> add(vector <int> a, vector <int> b) {
	vector <int> res;
	int t = 0, len = max(a.size(), b.size());
	
	for (int i = 0; i < len; i ++) {
		if (i < a.size()) t += a[i];
		if (i < b.size()) t += b[i];
		res.push_back(t % 10);
		t /= 10;
	}
	if (t) res.push_back(t);
	
	return res;
}

int main() {
	vector <int> a, b, c;
	a.push_back(1);
	b.push_back(1);
	
	for (int i = 3; ; i ++) {
		c = add(a, b);
		if (c.size() == 1000) {
			cout << i;
			return 0;
		}
		
		a.swap(b);
		b.swap(c);
	}
	
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main() {
	double x = sqrt(5);
	double a = log(x);
	double b = 999 * log(10);
	double c = log(0.5 + x / 2);
	
	int n = ceil((a + b) / c);
	cout << n;
	
	return 0;
}

/*
令,
p = ((1 + sqrt(5)) / 2) ^ n
q = ((1 - sqrt(5)) / 2) ^ n, abs(q) < 1 

f(n) = (p - q) / sqrt(5),约等于 p / sqrt(5)
根据f(n) > 10^999 
计算出n
*/
倒数的循环节
#include <bits/stdc++.h>
using namespace std;

int main() {
	int maxx = 0, ans;
	for (int i = 1; i < 1000; i ++) {
		int d = i;
		while (d % 2 == 0) d /= 2;
		while (d % 5 == 0) d /= 5;		
		
		if (d == 1) continue;
		
		for (int j = 1, t = 1; ; j ++) {
			t = (t * 10) % d;
			if (t == 1) {
				if (j > maxx) {
					maxx = j;
					ans = i;
				}
				break;
			}
		}
	}
	cout << ans;	

	return 0;
}
素数生成二次多项式
#include <bits/stdc++.h>
using namespace std;

bool num[120009];
vector <int> p;
int maxx, ans, x, y;

int main() {
	num[0] = true;
	for (int i = 2; i < 120000; i ++) {
		if (!num[i]) {
			p.push_back(i);
			for (int j = 2 * i; j < 120000; j += i) {
				num[j] = true;
			}
		}
	}
	
	for (int i = 1; i <= 168; i ++) {			//枚举b 
		int b = p[i];
		for (int a = -b; a < 1000; a += 2)  {	//枚举a	
			int n = 1;							//枚举n 
			while (true) {
				int x = n * n + a * n + b;
				if (x > 1 && !num[x]) {
					if (n > maxx) {
						maxx = n;
						ans = a * b;
//						x = a, y = b; 
					}
					n ++;
				}
				else {
					break;
				}
			}
		}
	}
	cout << ans;
	
	return 0;
}

/*
f(0) = b, 所以b一定是质数

如果 b = 2,f(n) = n * (n + a) + 2,当n为偶数时,f(n)为偶数,故b != 2
f(1) = 1 + a + b,所以a一定为奇数 
f(1) > 1,所以 a > -b
*/
螺旋数阵对角线
#include <bits/stdc++.h>
using namespace std;

int main() {
	int ans = 1;
	for (int i = 3; i <= 1001; i += 2) {
		ans += 4 * i * i - 6 * i + 6;
	}
	cout << ans;

	return 0;
}
不同的幂
#include <bits/stdc++.h>
using namespace std;

int main() {
	vector <double> v;
	
	for (int a = 2; a <= 100; a ++) {
		for (int b = 2; b <= 100; b ++) {
			double x = pow(a, b);
			v.push_back(x);
		}
	}
	sort(v.begin(), v.end());
	int ans = unique(v.begin(), v.end()) - v.begin();
	
	cout << ans << endl;

	return 0;
}
各位数字的五次幂
#include <bits/stdc++.h>
using namespace std;

int f[1000009];

int main() {
	int ans = 0;
	for (int i = 1; i < 1000000; i ++) {
		int t = i % 10;
		f[i] = f[i / 10] + t * t * t * t * t;
		if (i == f[i]) {
			ans += i;
		}		
	}
	cout << ans - 1;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值