uva10759 手算多项式分解 或者dp
来源:互联网 发布:编辑录音的软件 编辑:程序博客网 时间:2024/05/03 22:31
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef unsigned long long ULL;ULL a[555];ULL b[555];ULL gcd(ULL x, ULL y){ return y?gcd(y,x%y):x;}/**手算多项式分解把原题变为(x^1+x^2+x^3+x^4+x^5+x^6)^n 求指数大于等于x的项数的系数之和**/int main(){// freopen("data.in", "r", stdin); int n, x; ULL ans1, ans2; while(scanf("%d%d", &n, &x) != EOF && n+x){ memset(a, 0 ,sizeof a); for(int i = 1; i <= 6; i++) a[i] = 1; for(int k = 1; k < n; k++){ memset(b, 0, sizeof b); for(int i = k; i <= 6*k; i++){ for(int j = 1; j <= 6; j++){ b[i+j] += a[i]; } } memcpy(a, b, sizeof b); } ans1 = ans2 = 0; for(int i = 0; i <= n*6; i++){ if(i >= x) ans1 += a[i]; ans2 += a[i]; } if(ans1 == 0)puts("0"); else if(ans1 == ans2)puts("1");// else printf("%llu/%llu\n", ans1, ans2); else printf("%llu/%llu\n", ans1/gcd(ans1, ans2), ans2/gcd(ans1, ans2)); } return 0;}
dp的做法是用f[i][j]存投掷i次得分为j的概率 转移方程很简单 重点是用分数表示 记得用LCM GCD处理同分和约分 保证分数加减的正确性
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef unsigned long long ULL;ULL f[25][155], e[25][155];ULL sum1[25][155], sum2[25][155];ULL gcd(ULL x, ULL y){ return y?gcd(y,x%y):x;}ULL lcm(ULL x, ULL y){ return x/gcd(x,y)*y;}int main(){// freopen("data.in", "r", stdin); f[0][0] = e[0][0] = sum1[0][0] = sum2[0][0] = 1; for(int i = 1; i <= 24; i++){ for(int j = i; j <= 6*i; j++){ ULL FZ = 0, FM = 1, TMP; for(int k = max(0, j-6); k < j; k++){ if(f[i-1][k] == 0) continue; TMP = lcm(FM, e[i-1][k]*6); FZ = FZ*(TMP/FM) + f[i-1][k]*(TMP/(e[i-1][k]*6)); FM = TMP; TMP = gcd(FZ, FM); FZ /= TMP, FM /= TMP; } f[i][j] = FZ, e[i][j] = FM; if(i == j){ sum1[i][j] = f[i][j]; sum2[i][j] = e[i][j]; continue; } TMP = lcm(sum2[i][j-1], e[i][j]); FZ = sum1[i][j-1]*(TMP/sum2[i][j-1])+f[i][j]*(TMP/e[i][j]); FM = TMP; TMP = gcd(FZ, FM); sum1[i][j] = FZ/TMP, sum2[i][j] = FM/TMP; } } int n, x; ULL ans1, ans2; while(scanf("%d%d", &n, &x) != EOF && n+x){ if(x > 6*n) puts("0"); else if(x <= n) puts("1"); else{ ans1 = sum2[n][x-1] - sum1[n][x-1]; ans2 = sum2[n][x-1]; printf("%llu/%llu\n", ans1, ans2); } } return 0;}
0 0
- uva10759 手算多项式分解 或者dp
- UVA10759 - Dice Throwing(dp+gcd)
- matlab中多项式等表达式的因子分解或者提取公共因子的方法
- POJ2126 多项式分解定理
- uva10759(动态规划)
- 素数分解 dp
- uva10759 - Dice Throwing(掷骰子)
- USACO Factorials,DP,因子分解
- NBUT 1580 素数分解dp
- Lightoj1038 概率dp + 因数分解
- POJ 1221 整数分解 DP
- HDU1195 BFS或者DP
- [数论]整数与多项式环的唯一分解理论
- poj 2126 Factoring a Polynomial 数学多项式分解
- HDU 4447 Yuanfang, What Do You Think? 多项式分解
- 分解 多项式扩展 同幂合并 求方程
- (多项式特性4.7.7)POJ 2126 Factoring a Polynomial(判断一个多项式能否被分解)
- hdu 4495 Rectangle(多项式hash+二分+dp)
- Windows2000/XP启动过程详细分析
- java HashMap用自定义类做key(二)
- Uva 1335 Beijing Guards 解题报告(策略+二分)
- hdu 2717 Catch That Cow(bfs)
- 【深入Java虚拟机】之三:类初始化
- uva10759 手算多项式分解 或者dp
- 根据时间段,求出时间段范围内的每月最后一周 周二
- uva 299(排序、检索)
- 9 HashSet HashCode 迭代器 TreeSet Colletions类 HashMap
- struts2 简介
- Android 自定义圆圈进度并显示百分比例控件(纯代码实现)
- Svn 安装心得
- Oil Deposits 油田,深搜 http://vjudge.net/contest/view.action?cid=49515#problem/G
- IE 浏览器新奇技巧28则