3
26
2016
0

【数学】入门题

  1. 幂的阶乘【分解质因数】
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    const int inf = 0x3f3f3f3f;
    
    int main() {
      int t, n, m, cas = 1;
      scanf("%d", &t);
      while (t--) {
        scanf("%d%d", &m, &n);
        int i = 2;
        int ans = inf;
        while (m != 1) {
          int p = 0;
          while (m % i == 0) {
            m /= i;
            p++;
          }
          if (p) {
            int num = n;
            int tmp = 0;
            while (num) {
              tmp += num/i;
              num /= i;
            }
            ans = min(ans, tmp/p);
          }
          i++;
        }
        printf("Case %d:\n", cas++);
        if (ans)
          printf("%d\n", ans);
        else printf("Impossible to divide\n");
      }
      return 0;
    }
  2. 排列之和【】
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
typedef unsigned long long ll;
using namespace std;
const int maxn = 20;

int num[maxn], f[maxn];
int n;

void init() {
	f[0] = f[1] = 1;
	for (int i = 2; i < maxn; i++)
		f[i] = f[i-1] * i;
}

ll cal(int x) {
	ll tmp = 1;
	for (int i = 0; i < 10; i++)
		if (i == x)
			tmp *= f[num[i]-1];
		else tmp *= f[num[i]];
	return (ll)(f[n-1]) / tmp;
}

int main() {
	int a;
	init();
	while (scanf("%d", &n) != EOF && n) {
		memset(num, 0, sizeof(num));
		for (int i = 0; i < n; i++) {
			scanf("%d", &a);
			num[a]++;
		}
		ll sum = 0;
		for (int i = 0; i < 10; i++)
			if (num[i])
				sum += (ll)(i) * cal(i);
		ll ans = 0;
		for (int i = 0; i < n; i++)
			ans = ans * 10 + sum;
		printf("%lld\n", ans);
	}
	return 0;
}
Category: 数学相关 | Tags: 数论 | Read Count: 395

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com