UVa 766 Sum of powers (伯努利数)
题意: 求
,要求M尽量小。
析:这其实就是一个伯努利数,伯努利数公式如下:
伯努利数满足条件B0 = 1,并且
也有
几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 20 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} LL lcm(LL a, LL b){
return a * (b / gcd(a, b));
} struct Fraction{
LL mole;
LL deno;
Fraction() : mole(0), deno(1){ }
Fraction(LL m, LL d) : mole(m), deno(d) { sinal(); } void sinal(){
if(mole < 0 && deno < 0) mole = -mole, deno = -deno;
else if(mole >= 0 && deno < 0) mole = -mole, deno = -deno;
if(deno == 0) mole = 1;
} friend Fraction operator + (const Fraction &lhs, const Fraction &rhs){
LL l = lcm(lhs.deno, rhs.deno);
LL m = lhs.mole * (l/lhs.deno) + rhs.mole * (l/rhs.deno);
return Fraction(m, l);
} friend Fraction operator - (const Fraction &lhs, const Fraction &rhs){
LL l = lcm(lhs.deno, rhs.deno);
LL m = lhs.mole * (l/lhs.deno) - rhs.mole * (l/rhs.deno);
return Fraction(m, l);
} friend Fraction operator * (const Fraction &lhs, const Fraction &rhs){
LL m = lhs.mole * rhs.mole;
LL d = lhs.deno * rhs.deno;
LL g = gcd(m, d);
return Fraction(m / g, d / g);
} friend Fraction operator / (const Fraction &lhs, const Fraction &rhs){
LL m = lhs.mole * rhs.deno;
LL d = lhs.deno * rhs.mole;
LL g = gcd(m, d);
return Fraction(m / g, d / g);
} void print(){
printf("%lld / %lld\n", mole, deno);
}
}; Fraction C[maxn][maxn];
Fraction B[maxn]; void init(){
for(int i = 0; i < 25; ++i)
C[i][0] = C[i][i] = Fraction(1, 1);
for(int i = 2; i < 25; ++i)
for(int j = 1; j < i; ++j)
C[i][j] = C[i-1][j] + C[i-1][j-1];
B[0] = Fraction(1, 1);
for(int i = 1; i < 23; ++i){
for(int j = 0; j < i; ++j)
B[i] = B[i] + C[i+1][j] * B[j];
B[i] = B[i] * Fraction(-1LL, i+1LL);
}
} Fraction ans[maxn]; int main(){
init();
int T; cin >> T;
while(T--){
scanf("%d", &n);
LL l = 1;
for(int i = 1; i <= n+1; ++i){
ans[i] = C[n+1][i] * B[n+1-i] * Fraction(1LL, n+1LL);
l = lcm(l, ans[i].deno);
}
ans[n] = ans[n] + Fraction(1LL, 1LL);
printf("%lld", l);
for(int i = n+1; i > 0; --i)
printf(" %lld", l / ans[i].deno * ans[i].mole);
printf(" 0\n");
if(T) printf("\n");
}
return 0;
}
UVa 766 Sum of powers (伯努利数)的更多相关文章
- [伯努利数] poj 1707 Sum of powers
题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS Memory Lim ...
- UVA 10622 - Perfect P-th Powers(数论)
UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...
- [CSAcademy]Sum of Powers
[CSAcademy]Sum of Powers 题目大意: 给定\(n,m,k(n,m,k\le4096)\).一个无序可重集\(A\)为合法的,当且仅当\(|A|=m\)且\(\sum A_i=n ...
- Euler's Sum of Powers Conjecture
转帖:Euler's Sum of Powers Conjecture 存不存在四个大于1的整数的五次幂恰好是另一个整数的五次幂? 暴搜:O(n^4) 用dictionary:O(n^3) impor ...
- 【POJ1707】【伯努利数】Sum of powers
Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...
- UVA766 Sum of powers(1到n的自然数幂和 伯努利数)
自然数幂和: (1) 伯努利数的递推式: B0 = 1 (要满足(1)式,求出Bn后将B1改为1 /2) 参考:https://en.wikipedia.org/wiki/Bernoulli_numb ...
- POJ 1707 Sum of powers(伯努利数)
题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...
- uva 11752 The Super Powers 素数+大数判断大小
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- UVA 10622 Perfect P-th Powers
https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include& ...
随机推荐
- UVA - 1603 Square Destroyer (DLX可重复覆盖+IDA*)
题目链接 给你一个n*n的由火柴组成的正方形网格,从中预先拿掉一些火柴,问至少还需要拿掉多少火柴才能破坏掉所有的正方形. 看到这道题,我第一反应就是——把每根火柴和它能破坏掉的正方形连边,不就是个裸的 ...
- 基于JQ的三级联动菜单选择
<!-- author:青芒 --> <!DOCTYPE html> <html lang="en"> <head> <met ...
- 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT
题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...
- AngularJS:事件
ylbtech-AngularJS:事件 1.返回顶部 1. AngularJS 事件 AngularJS 有自己的 HTML 事件指令. ng-click 指令 ng-click 指令定义了 Ang ...
- stdin和STDIN_FILENO的区别
STDIN_FILENO与stdin的区别: STDIN_FILENO: 1).数据类型:int 2).层次:系统级的API,是一个文件句柄,定义在<unistd.h>中. 3).相应的函 ...
- Socket编程, 在server端read()函数调用后显示错误:Transport endpoint is not connected (犯了低级错误)
for(;;){ socklen_t len = sizeof(client_address); connfd = accept(listenfd, (struct sockaddr *)&c ...
- tomcat 三种部署方式以及server.xml文件的几个属性详解
一.直接将web项目文件件拷贝到webapps目录中 这是最常用的方式,Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用.如果你想要修改这个默认 ...
- leetcode443
使用两个数组分别记录字符和对应的数字,然后清除原来的vector,重新向里面添加元素.注意判断1个字符时,不将'1'加入vector. int compress(vector<char>& ...
- C#如何拿到从http上返回JSON数据?
第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) 第四章:C#如何生成JSON字符串提交给接口 ...
- dubbo错误排查之No provider available for the service
今天搞的一个dubbo服务,暴漏出来了,但是consumer端启动就报这个错,排查过程记录一下 一.启动zkCli 利用命令查看 ls / ls /dubbo 继续查看 ls /dubbo/com.w ...