UVA 766 - Sum of powers(伯努利数)

766 - Sum of powers

题意:求    转化成 的各系数

思路:在wiki看了伯努利数的性质,

 可以推成 

然后B为伯努利数,有公式

如此一来就可以去递推求出每项伯努利数了,然后在根据n去通分,求出每一项的答案,中间过程用到了分数的运算。

代码:

#include <stdio.h>
#include <string.h>

long long gcd(long long a, long long b) {
	if (!b) return a;
	return gcd(b, a % b);
}

long long lcm(long long a, long long b) {
	a = a / gcd(a, b) * b;
	if (a < 0) a = -a;
	return a;
}

struct Fraction {
	long long a, b;
	Fraction() {a = 0; b = 1;}

 	Fraction(long long x) {
		a = x; b = 1;
 	}

 	Fraction(long long x, long long y) {
 		a = x; b = y;
  	}

  	void deal() {
  		if (b < 0) {b = -b; a = -a;}
  		long long k = gcd(a, b);
  		if (k < 0) k = -k;
  		a /= k; b /= k;
    }

    Fraction operator+(Fraction p) {
    	Fraction ans;
    	ans.b = lcm(b, p.b);
    	ans.a = ans.b / b * a + ans.b / p.b * p.a;
    	ans.deal();
    	return ans;
   	}

   	Fraction operator-(Fraction p) {
    	Fraction ans;
    	ans.b = lcm(b, p.b);
    	ans.a = ans.b / b * a - ans.b / p.b * p.a;
    	ans.deal();
    	return ans;
   	}

   	Fraction operator*(Fraction p) {
   		Fraction ans;
   		ans.a = a * p.a;
   		ans.b = b * p.b;
   		ans.deal();
   		return ans;
   	}

   	Fraction operator/(Fraction p) {
   		Fraction ans;
   		ans.a = a * p.b;
   		ans.b = b * p.a;
   		ans.deal();
   		return ans;
   	}

   	void operator=(int x) {
   		a = x;
   		b = 1;
   	}

   	void print() {
   		printf("%lld/%lld\n", a, b);
    }
};

const int N = 25;

Fraction B[N], C[N][N], a[N];
int n, t;
long long L;

void init() {
	for (int i = 0; i < N; i++) {
		C[i][0] = 1;
		C[i][i] = 1;
		for (int j = 1; j < i; j++) {
			C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
		}
 	}
	B[0] = 1;
	for (int i = 1; i <= 20; i++) {
		B[i] = 0;
		for (int j = 0; j < i; j++) B[i] = B[i] - C[i + 1][j] * B[j];
		B[i] = B[i] / C[i + 1][i];
 	}
}

int main() {
	init();
	scanf("%d", &t);
	while (t--) {
		L = 1;
		scanf("%d", &n);
		for (int i = 0; i <= n; i++) {
			a[i] = C[n + 1][i] * B[i] * Fraction(1, n + 1);
			L = lcm(L, a[i].b);
  		}
  		printf("%lld ", L);
  		a[1] = a[1] + 1;
  		for (int i = 0; i <= n; i++)
  			printf("%lld ", L / a[i].b * a[i].a);
		printf("0\n");
		if (t) printf("\n");
 	}
	return 0;
}

UVA 766 - Sum of powers(伯努利数)

时间: 2024-11-10 11:41:42

UVA 766 - Sum of powers(伯努利数)的相关文章

uva 766 - Sum of powers(数学+递推)

题目连接:uva 766 - Sum of powers 题目大意:将Sk(n)=∑i=1nik化简成Sk(n)=ak+1nk+1+aknk+?+a0M 解题思路: 已知幂k,并且有(n+1)k=C(kk)nk+C(k?1k)nk?1+?+C(0k)n0结论. 所以令 (n+1)k+1?nk+1=C(kk+1)nk+C(k?1k+1)nk?1+?+C(0k+1)n0 nk+1?(n?1)k+1=C(kk+1)(n?1)k+C(k?1k+1)(n?1)k?1+?+C(0k+1)(n?1)0 - 2

[伯努利数] poj 1707 Sum of powers

题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 735   Accepted: 354 Description A young schoolboy would like to calculate the sum for some fixed natural k and different

UVA 10622 - Perfect P-th Powers(数论)

UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取所有质因子个数的gcd就是答案,但是这题有个坑啊,就是输入的可以是负数,负数的情况比较特殊,p只能为奇数,这时候是要把答案不断除2除到为奇数即可. 代码: #include <stdio.h> #include <string.h> #include <math.h> long long n; int prime[333333], vi

UVA 1210 Sum of Consecutive Prime Numbers(数论)

UVA - 1210 Sum of Consecutive Prime Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such

uva 10290 {Sum+=i++} to Reach N (数论-整数和素数)

Problem H {sum+=i++} to Reach N Input: standard input Output:  standard output Memory Limit: 32 MB All the positive numbers can be expressed as a sum of one, two or more consecutive positive integers. For example 9 can be expressed in three such ways

UVA - 11752 The Super Powers

We all know the Super Powers ofthis world and how they manage to get advantages in political warfare or evenin other sectors. But this is not a political platform and so we will talkabout a different kind of super powers – "The Super Power Numbers&qu

UVA766 Sum of powers(1到n的自然数幂和 伯努利数)

自然数幂和: (1) 伯努利数的递推式: B0 = 1 (要满足(1)式,求出Bn后将B1改为1 /2) 参考:https://en.wikipedia.org/wiki/Bernoulli_number http://blog.csdn.net/acdreamers/article/details/38929067 使用分数类,代入求解 #include<cstdio> #include<iostream> #include<cstdlib> #include<

1^k+2^k+3^k+&#183;&#183;&#183; ZOJ 3547 UVA 766

题目链接:点击打开链接 祭出结论:点击打开链接 资料:组合数回代公式:点击打开链接 伯努利数:点击打开链接 点击打开链接 方法一: 首先给出一个神奇的组合数公式: C(n,k)+C(n+1,k)+C(n+2,k)+C(n+3,k)--+C(N,k) 由于: C(n,k)=C(n-1,k)+C(n-1,k-1) 因此 上式  =  - C(n,k+1) +  {  C(n,k+1)+C(n,k)   }  +  C(n+1,k)-- =  - C(n,k+1) +  {C(n+1,k+1}  + 

UVA 10622 Perfect P-th Powers

https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include<cmath> #include<cstdio> #include<algorithm> #define N 65550 using namespace std; int gcd(int a,int b) { return !b ? a : gcd(b,a%b); } int