随笔分类 - 数学
hdu 4609 3-idiots
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4609FFT 不会 找了个模板代码:#include #include #include #include #include using namespace std;typedef long long ll;typedef pairppd;const double PI = acos(-1.);const int MAXL = (1 >2)>=n) ln=ln>>1; id = 0; fill0(1,0,a,A); fft(1.0,A); id = 0; fill0(1,0,b,...
阅读全文
hdu 4602 Partition
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4602输入 n 和 k首先 f(n)中k的个数 等于 f(n-1) 中 k-1的个数最终等于 f(n-k+1) 中 1 的个数舍 s(n) = f(n) + f(n-1) + ....+ f(1)则 f(n) = s(n) - s(n-1)由于 s(n) = s(n-1) + 2^(n-2) + s(n-1) = 2*(s(n-1)) + 2^(n-2) = 2^(n-1) + (n-1)*2^(n-2) = (n+1)*2^(n-2)代码:#include#include#include#inclu...
阅读全文
UVa 10870 - Recurrences
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1811矩阵快速幂代码:#include#include#include#include#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;typedef long long ll;typedef unsigned int
阅读全文
UVa 11762 - Race to 1
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2862晕 多加了一个# wa了N久 细节呀代码及其注释:#include#include#include#include#include#include#include#include#include#include#include#define ull unsigned long long#define ll long long#define lint long longusin
阅读全文
UVa 11427 - Expect the Expected
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2422每一天的情况是相互独立的d[i][j] 表示这一天比了i次赢了j次还不能回去的概率这样就可以 求出比了n次 仍然不能回去(垂头丧气回去,以后再也不玩了)的概率 Q然后可以经过推导 最终期望为 1/Q代码:#include#include#include#include#include#include#include#include#include#include#includ
阅读全文
UVa 11021 - Tribles
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1962f(i) 表示 在有一只tribble 经过i天后全死掉的 概率然后枚举tribble第一天生了多少个后代 所以:f(i) = P0*(f(i-1)^0)+P1*(f(i-1)^1)+P2*(f(i-1)^2)+.......+Pn-1*(f(i-1)^n-1)代码:#include#include#include#include#include#include#includ
阅读全文
UVa 11426 - GCD - Extreme (II)
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2421代码及其注释:#include #include #include #include #include #include #include #define ll long long#define lint long longusing namespace std;const int N=4000005;int phi[N];//phi[k] 表示从1到k 和k互质的数有几个l
阅读全文
LA 4119 - Always an integer
摘要:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2120假设式子的最高幂次为k 则只需要测试1到k+1就可以了原理见 刘汝佳的《算法竞赛入门经典入门指南》 P123字符串需要自己处理,比较繁琐 一定要细心代码:#include #include #include #include #include #include #include #define ll long long#define lint long lon
阅读全文
UVa 10253 - Series-Parallel Networks
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1194不看解析还真没有什么思路,刘汝佳的《算法竞赛入门经典训练指南》P117将原问题转换成树 然后再进行树型DP代码:#include #include #include #include #include #include #include #define ll long longusing namespace std;const int N=100;ll dp[N][N];ll
阅读全文
LA 4123 - Glenbow Museum
摘要:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2124这道题可以用递推做,但是没有必要首先说长度为L假设R的个数为x,O的个数为y,那么x+y=L而且x-y=4当L为奇数和小于4的时候肯定不可以由于O不能相邻 可以假设O 的数目和R一样多,交叉排放。然后从这些O中去掉4个1,R开头的情况 C(x,4)2,O开头的情况C(x-1,4)然后两种情况相加即可代码:#include #include #include #
阅读全文
UVa 11174 - Stand in a Line
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2115数学的特点在于不断的推导,此题还需要用到欧拉定理和逆元的相关性质,推荐博客(有部分小错误):http://www.cnblogs.com/vongang/archive/2013/06/04/3117370.html代码:#include #include #include #include #include #include #include #define ll long
阅读全文
UVa 11137 - Ingenuous Cubrency
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2078简单递推代码:#include #include #include #include #include #include #define ll long longusing namespace std;const int N=10005;ll sum[N][50];int main(){ //freopen("data.in","r",
阅读全文
UVa 11375 - Matches
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2370难题锻炼思维,水题锻炼细心。这个题有两个需要注意的地方1,关于前导零2,高精度代码:import java.math.BigInteger;import java.util.Scanner;public class Main { /** * @param args */ static final int N=2005; public static void main(Stri
阅读全文
Uva 11401 - Triangle Counting
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2396要注意细节代码:#include #include #include #include #include #include #define ll long longusing namespace std;const int N=1000005;ll sum[N];int main(){ //freopen("data.in","r",s
阅读全文
Uva 11538 - Chess Queen
摘要:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2533公式推导代码:#include #include #include #include #include #include #define ll long longusing namespace std;int main(){ //freopen("data.in","r",stdin); ll n,m; while(cin>>
阅读全文
sdut 2605 A^X mod P
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2605这个题卡的是优化,直观解法是在求x^y时 用快速羃,但会TLE快速羃其实就是把 y 变成了二进制 这样每求一次花费为 y 的二进制长度我又把 y 变成了十进制进行求解 虽然优化了 但还是超时 优化的不够好,看了一下题解我把 y 变成100000 进制 这样 y 的100000进制长度就变成了 2 只要事先打表,就可以快很多了 。代码:#include<iostream>#include<cstdio>#includ
阅读全文
SPOJ 4191. Sky Code
摘要:http://www.spoj.com/problems/MSKYCODE/容斥原理在我不断的优化过后,终于过了代码:#include <iostream>#include <cstdio>#include <map>#include <algorithm>#include <cstring>#include <string>#include <vector>#define ll long longusing namespace std;const int N=10005;const long long LIN
阅读全文
TopCoder SRM 457 TheHexagonsDivOne
摘要:容斥 组合排列代码:#include <iostream>#include <cstdio>#include <map>#include <algorithm>#include <cstring>#include <string>#define ll long longusing namespace std;const int N=1005;const long long LINF=(long long)(1e18);ll c[N][N];ll a[N];void FC(int n){ for(int i=0;i<=
阅读全文
TopCoder SRM 176 Deranged
摘要:代码:#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<set>#include<map>#include<cmath>#include<algorithm>#include<vector>#include<cmath>#include<queue>#include<stack>//#define ull unsigned long long#defi
阅读全文
TopCoder SRM 382 CharmingTicketsEasy
摘要:这题的主要算法是DP 只在最后结果上用了一下容斥原理代码:#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<set>#include<map>#include<cmath>#include<algorithm>#include<vector>#include<cmath>#include<queue>#include<stack>//#define u
阅读全文
浙公网安备 33010602011771号