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& ...
随机推荐
- NOI 模拟赛 #3
打开题一看,咦,两道数数,一道猫式树题 感觉树题不可做呀,暴力走人 数数题数哪个呢?感觉置换比矩阵好一些 于是数了数第一题 100 + 0 + 15 = 115 T1 bishop 给若干个环,这些环 ...
- LeetCode 333. Largest BST Subtree
原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ 题目: Given a binary tree, find the largest ...
- Unity3D的SystemInfo类,用于获取运行设备硬件信息(CPU、显卡、类型等)
SystemInfo类中的静态变量: 中文显示: Rendering.CopyTextureSupport copyTextureSupport:(只读)支持多种复制纹理功能的情况. string ...
- 【转】深入剖析Java中的装箱和拆箱
深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...
- flask+blueprint路由配置
1.flask默认的静态文件和html文件在app应用文件夹里的相应文件夹下:app // Flask||--static ||--templates |静态文件默认的url地址为:url_prefi ...
- (转)C# -- 扩展方法的应用(Extension Methods)
本文转载自:http://blog.csdn.net/zxz414644665/article/details/9793205 当你有下面这样一个需求的时候,扩展方法就会起到作用:在项目中,类A需要添 ...
- SQL Server获取TEXT字段的内容长度
DATALENGTH 返回任何表达式所占用的字节数. 语法 DATALENGTH ( expression ) 参数 expression 任何类型的表达式. 返回类型 int 注释 DATALENG ...
- 愿天下有情人都是失散多年的兄妹(bfs)
L2-016. 愿天下有情人都是失散多年的兄妹 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 呵呵.大家都知道五服以内不得通婚 ...
- [JAVA反序列化DEMO]利用RMI进行反序列化一键启动工具
功能: 命令行启动jar包,用户自定义启动RMI端口.默认内置Apache Commons Collections.只需一键启动即可测试java反序列化漏洞. 启动服务: [root@sevck_v3 ...
- Java连接mysql数据库攻略
一. 软件下载 Mysql 下载版本:4.1.11 http://dev.mysql.com/downloads/mysql/4.1.html JDBC驱动 下载版本:3.1.8 http://dev ...