
数学
sepNINE
it is written
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
poj 2440 DNA 递推在模下存在循环节
题意和分析: 题意都是废话,用动态规划列个方程化下简,最后也是这题有意思的地方是求a(n)=a(n-1)+a(n-3)+a(n-3) 在模2005下的值。 你可能会说,这个简单啊,弄个数组,搞个for循环就行啊,没错,但这是O(n)。能快点么? 嗯,那用矩阵相乘表示递推,在乘法矩阵结合律的保证下幂运算加速足够快了吧?嗯,是快不少,但这是O(logn)原创 2017-07-28 23:19:50 · 572 阅读 · 0 评论 -
poj 3066 Maximum 贪心
//poj 3066 //sep9 #include #include using namespace std; int main() { double m,p,a,b; while(scanf("%lf%lf%lf%lf",&m,&p,&a,&b)==4){ double min_x=-1/sqrt(a); double max_x=sqrt(a); double sum=原创 2015-11-16 10:35:55 · 492 阅读 · 0 评论 -
poj 1411 Calling Extraterrestrial Intelligence Again 筛素数
//poj 1411 //sep9 #include using namespace std; const int MAX=50024; bool vis[MAX+10]; int primes[10024],num; int main() { memset(vis,false,sizeof(vis)); num=0; for(int i=2;i<MAX;++i){ if(!vis[原创 2015-10-13 16:27:53 · 497 阅读 · 0 评论 -
poj 1320 Street Numbers 解pell方程
题意: 解(2*n+1)^2-8*a^2=1. 分析: 令x=2*n+1,y=a,则转化为x^2-8*y^2=1的pell方程。pell方程详解 代码: //poj 1320 //sep9 #include using namespace std; int main() { int x1=3,y1=1,t=10; while(t--){ int x2,y2; x2=3*原创 2015-11-12 15:20:21 · 504 阅读 · 0 评论 -
poj 2409 Let it Bead 项链旋转+对称群的polya计数
//poj 2409 //sep9 #include using namespace std; typedef __int64 INT; INT pow[32]; INT gcd(INT a,INT b) { return a%b==0?b:gcd(b,a%b); } int main() { INT m,n; while(scanf("%I64d%I64d",&m,&n)==2){原创 2015-11-11 11:24:46 · 491 阅读 · 0 评论 -
poj 2888 Magic Bracelet polya计数+矩阵统计路径数
题意: 用m种颜色涂n个小球组成的项链,其中k对颜色不能相邻,求一共有多少种方案。 分析: polya计数,难点是对于一个置换p,怎样确定在p下等价的方案数。设p的循环节数为i,如果没有限制显然是m^i,有限制的话怎么办呢?考虑将每一种方案转化为路径,比如a->b->c->a,那么循环节数为i的方案数就是a到a加上b到b...长为i的不同路径数。. 代码: //原创 2015-11-11 15:30:50 · 424 阅读 · 0 评论 -
poj 1286 Necklace of Beads 项链旋转+对称群的polya计数
//poj 1286 //sep9 #include using namespace std; typedef __int64 INT; INT pow3[32]; INT gcd(INT a,INT b) { return a%b==0?b:gcd(b,a%b); } int main() { pow3[0]=1; for(INT i=1;i<24;++i) pow3[i]=pow原创 2015-11-11 11:09:12 · 397 阅读 · 0 评论 -
poj 2154 Color polya计数+欧拉优化
//poj 2154 //sep9 #include using namespace std; const int maxN=36000; int p; int prime[maxN+10],vis[maxN+10]; int phi(int n) { int res=n; for(int i=0;prime[i]*prime[i]<=n;++i){ if(n%prime[i]==0)原创 2015-11-11 00:32:01 · 421 阅读 · 0 评论 -
poj 2748 Logs Stacking 递推公式的循环节
题意: 求f(n)=f(n-1)+2*f(n-2)+3*f(n-3)...(n-1)*f(1)+f(0),f(0)=f(1)=1的值。 分析: 转换为f(n)=3*f(n-1)-f(n-2)的二阶递推,用矩阵乘法可以做到logn的复杂度,可是由于测试用例数多和n实在太大,logn也超时。。其实n阶递推在模下是存在循环节的,设循环节=t,y=x+t,则f(x)=f(y),f(x+1)=f(y+原创 2015-11-06 10:51:18 · 1246 阅读 · 0 评论 -
poj 2635 The Embarrassed Cryptographer 筛素数+高精度除法
对两种筛素数的方法给出对比。原创 2015-08-03 03:58:28 · 1072 阅读 · 0 评论 -
poj 3406 Last digit 求组合数的最后非零数
题意: 求组合数C(n,m),n>=m的最后非0数。 分析: 与求排列数的最后非0数类似,要注意分母上以3结尾的数可能比分子上的多,要特殊处理。 代码: //poj 3406 //sep9 #include using namespace std; int table[4][4]={ 6,2,4,8, 1,3,9,7, 1,7,9,3, 1,9,1,9, }; int f2(原创 2015-11-16 12:45:46 · 697 阅读 · 0 评论 -
poj 2773 Happy 2006 容斥原理+筛素数+二分
//poj 2773 //sep9 #include using namespace std; const int MAX=1000; int n,k,cnt,t; bool vis[MAX+10]; int prime[MAX+10],a[MAX+10]; void get_prime() { int tmp=n; t=0; for(int i=0;i<cnt&&prime[i]*pr原创 2015-11-03 10:07:21 · 534 阅读 · 0 评论 -
poj 2649 Factovisors 对n!进行因数分解
//poj 2649 //sep9 #include #include using namespace std; const int MAXN=50000; bool vis[MAXN+10]; int prime[MAXN+10]; int n,m,p; vector > exp; void init() { memset(vis,0,sizeof(vis)); p=0; for(i原创 2015-12-12 23:49:54 · 656 阅读 · 0 评论 -
poj 1079 Ratio 模拟
模拟题,只是涉及分数精度的一个技巧:c++中如何取整?如果a是一个浮点数,int b=(int)a; b是a的整数部分么?? 答案是不一定,因为在一般实际的算法中,不是int b=(int)a这么简单 a可能经过了很多运算,有精度丢失。比如a的真值是8 计算机不小心搞成了7.999999或8.000001这样直接取整就会有问题(假设我们的计算机误差范围不超过1e-6,计算机不能保原创 2017-07-09 22:14:07 · 565 阅读 · 0 评论 -
poj 3511 Fermat's Christmas Theorem 筛素数
//poj 3511 //sep9 #include using namespace std; const int max_prime_num = 1000010; const int max_n = 1000010; int primes[max_prime_num]; int vis[max_n],num; void init() { memset(vis,0,sizeof(vis原创 2016-12-11 18:15:01 · 560 阅读 · 0 评论 -
poj 1091 跳蚤 扩展欧几里得+容斥原理
//poj 1091 //sep9 #include using namespace std; typedef long long ll; ll n,m,p[128]; int cnt; void init() { cnt=0; ll mm=m; for(ll i=2;i*i<=mm;++i){ if(mm%i==0){ p[cnt++]=i; while(mm%i==原创 2016-05-03 15:37:52 · 576 阅读 · 0 评论 -
poj 3372 Candy Distribution 数论
题意: 设f(x)=(x+1)*x/2mod(n),问f(x)是否可以构成n的一个完全剩余系。 分析: 网上很多证明一开始就说f(x)的周期为n,这是不对的。当n是奇数时f(x)周期T=n,当n为偶数时T=2*n,f(x+n)=f(x)+n/2。依据这3个性质可推出f(x)可以构成n的一个完全剩余系的充要条件是n是2的正整数数幂。 代码: //poj 3372 //sep9 #inclu原创 2016-04-17 16:45:35 · 450 阅读 · 0 评论 -
poj 3244 Difference between Triplets 最值公式推导
题意: 给n个三元组,求任意一对i,j三元组d(i,j)的和,d(i,j)=max(ai-aj,bi-bj,ci-cj)-min(ai-aj,bi-bj,ci-cj);ai就是三元组i的第一个值,bi就是三元组i的第二个值。 分析: 首先要知道max(n,m)=n+m+|n-m|,min(n,m)=a+b-|a-b|,max(n,m,k)-min(n,m,k)=|n-m|-|n-k|-|m-原创 2016-04-15 17:57:03 · 730 阅读 · 0 评论 -
poj 1023 The Fun Number System 整数的表示
题意: 给一个整数n,求它是否可以被一个k位的01串表示,这个01串与整数的二进制表示类似,只不过每位表示的正负由一个输入字符串s给出。 分析: 理解了整数的唯一任意进制表示是由整数对任意数求商求余唯一这一公理导出之后这题就不难了,这题的细节是注意负数求余:-5/2=-2,-5%2=-1。 代码: //poj 1023 //sep9 #include #include using n原创 2016-04-14 15:45:45 · 650 阅读 · 0 评论 -
poj 1365 Prime Land 素因数分解
//poj 1365 //sep9 #include #include using namespace std; const int MAXN=32767; int tot,vis[MAXN+10],prime[MAXN+10]; char s[128]; void solve(int sum) { --sum; stack > sta; for(int j=0;j<tot;++j)原创 2015-12-03 22:02:01 · 500 阅读 · 0 评论 -
poj 2273 An Excel-lent Problem 类进制转换
//poj 2273 //sep9 #include using namespace std; int r,c; char ans[128],ch; int sum[32]; int main() { sum[0]=0; sum[1]=26; int p=26; for(int i=2;i<9;++i){ p*=26; sum[i]=sum[i-1]+p; } while(s原创 2015-11-18 23:54:31 · 834 阅读 · 0 评论 -
poj 2188 Cow Laundry 求逆序数水题
//poj 2188 //sep9 #include using namespace std; struct NODE { int x,y; }a[1024]; int ans[1024]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d%d",&a[i].x,&a[i].y); for(int原创 2015-11-19 12:18:07 · 700 阅读 · 0 评论 -
poj 3105 Expectation 按位统计
题意: 给n,求sum(i^j)/(n^2),0 分析: 暴力n^2算法肯定超时。这是logn按位统计算法:按位先算出0出现的个数x,则1出现的个数为n-x,再算每位对和的贡献。 代码: //poj 3105 //sep9 #include using namespace std; int main() { int cases; scanf("%d",&cases); whil原创 2015-07-05 21:27:37 · 926 阅读 · 0 评论 -
poj 1845 Sumdiv 矩阵法求幂的和
题意: 给A,B,求A^B所有因子模9901的和(0 分析: 因素分解后关键是求1+p^1+p^2+....p^k的和,因为等比数列和公式涉及除法,不用乘法逆元的话不能直接用公式。设s(k+1)=p^0+p^1+...p^k,s0=0,则 然后就能2分幂快速求了。 代码:原创 2015-07-08 11:40:49 · 767 阅读 · 0 评论 -
poj 1671 Rhyme Schemes 第二类Stirling数
题意: 求s(n,0)+s(n,1)+...s(n,n),s(i,j)为第二类Stirling数。 分析: 有递推公式s(p,k)=(p-1)*s(p-1,k)+s(p-1,k-1) ,1 代码: //poj 1671 //sep9 #include using namespace std; double s[64][64]; double sum[64]; int main() {原创 2015-07-01 09:33:02 · 878 阅读 · 0 评论 -
poj 3014 Cake Pieces and Plates 整数拆分
题意: 将m拆成n个数,允许某个数为0,求拆分方案数。 分析: 裸的整数拆分,设h(m,n)表示将m拆成n个数,允许某数为0的方案数,递推方程见代码。很有意思的是,参考上一篇写poj1221的博文中,设f(m,n)表示将m进行任意份数不允许有0的整数拆分,且最大元素小于等于m的方案数,则h(m,n)==f(m,n)。。。。求解此等式意义。。。 代码: //poj 3014 //sep9原创 2015-04-06 21:18:06 · 887 阅读 · 0 评论 -
poj 3970 Party 最小公倍数
题意: 给n个数,求它们的最小公倍数。 分析: lcm(a,b)==a*b/gcd(a,b); 代码: //poj 3970 //sep9 #include using namespace std; typedef long long ll; ll gcd(ll a,ll b) { return a%b==0?b:gcd(b,a%b); } int main() { int n;原创 2015-04-03 22:52:21 · 1391 阅读 · 0 评论 -
poj 3911 Internet Service Providers 解一元二次方程
少有人做的水题,直接贴代码。 //poj 3911 //sep9 #include using namespace std; typedef long long ll; int main() { ll n,c; while(scanf("%lld%lld",&n,&c)==2){ if(n==0){ printf("0\n"); continue; } ll原创 2015-04-03 01:08:44 · 1181 阅读 · 0 评论 -
poj 2126 Factoring a Polynomial 数学多项式分解
题意: 给一个多项式,求它在实数域内的可分解性。 分析: 代数基本定理。 代码: //poj 2126 //sep9 #include using namespace std; int main() { int n; scanf("%d",&n); int a,b,c; scanf("%d%d%d",&a,&b,&c); if(n>2) puts("NO"); els原创 2015-03-16 21:57:36 · 1115 阅读 · 0 评论 -
poj 1959 Darts 允许重复组合
水题,直接贴代码。 //poj 1959 //sep9 #include using namespace std; int n; int f[128]; int solve() { int x,y,z,sum=0; for(x=0;x<=n&&x<=60;++x) for(y=x;y<=n&&y<=60;++y) for(z=y;z<=n&&z<=60;++z) if(x原创 2015-03-19 07:39:30 · 963 阅读 · 0 评论 -
poj 3292 Semi-prime H-numbers 筛数打表
题意: 给定n,求1至n中有多少个数能仅表示成两个(4*x+1,x>=1)的数之积。 分析: 打表,一开始的list[i*j]代表i*j的组成方式数,不需要用乘法,加法就好。 代码: //poj 3292 //sep9 #include using namespace std; int list[1200000]; int main() { memset(list,0,sizeof原创 2015-01-22 13:44:34 · 759 阅读 · 0 评论 -
poj 2115 C Looooops 扩展欧几里得
题意: 在while(x=a;x!=b;x+=c) statement;中,问statement会被执行多少次,计算在2^k下进行。 思路: 等价于计算同余式a+c*x=b(mod2^k)用扩展欧几里得算法。设g=gcd(a,b)在计算a*x+b*y=g过程中,x的结果可以用b/g调整,y的结果可以用a/g调整,因为a*(b/g)==b*(a/g)。 代码: //poj 2115 //s原创 2014-12-26 19:07:19 · 793 阅读 · 0 评论 -
poj 3734 Blocks 组合计数
题意: 给长度为n的串,串中每位可用0,1,2,3,求0的个数和1的个数都为偶数的串数。 思路: 组合计数,4^n=(2+1+1)^4,ans=sum[i=0...n](c(n,i)2^i*sum[k=n-i且为偶数,t=0,2,...k](c(k,t) );最后推出4^(n-1)+2^(n-1)。 代码: //poj 3734 //sep9 #include using name原创 2014-12-23 10:21:24 · 597 阅读 · 0 评论 -
poj 1730 Perfect Pth Powers 精度修正
题意: 给数x,求最大的整数p,使对某个整数b有x==b^p。 分析: 从大往小枚举p看是否找得到相应的b,当确定p后b==pow(x,1/p),算pow(x,1/p)肯定是会有误差的,解决的办法是将b,b+1,b-1分别带入验算,只要有一个满足我们就找到了相应的b。 代码: //poj 1730 //sep9 #include #include using namespace st原创 2014-12-02 11:20:28 · 625 阅读 · 0 评论 -
poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS 整数拆分
题意: 给一个数n,求有多少种和为n的单峰先增序列,比如当n=5时结果为3:(5), (1 3 1), (1 1 1 1 1)。 分析: 转化为求类似整数拆分问题,f(i,j)的意义是把i进行拆分,最大数小于等于j的方法数。 代码: //poj 1221 //sep9 #include using namespace std; const int maxN=300; __int64 a原创 2015-04-06 20:23:47 · 1038 阅读 · 0 评论 -
poj 3587 The Biggest Cake 正弦定理
题意: 给n个点,求其中3个点构成三角形的最大外接圆半径。 分析: 正弦定理,A/sina=B/sinb=C/sinc=ABC/(2*s)=2*R。 代码: //poj 3587 //sep9 #include #include using namespace std; const int maxN=700; double dis[maxN][maxN]; double x[maxN原创 2015-04-08 12:11:51 · 1243 阅读 · 6 评论 -
poj 3286 How many 0's? 按位统计
题意: 给m 分析: 按位算当某位是0时左边有多少种情况,右边有多少种情况,注意左边的情况数为-1时(这时遍历到最高位)是为了把右边多加的情况减去,也就是把0作为开头时的情况减去。 代码: //poj 3286 //sep9 #include using namespace std; typedef __int64 ll; ll b[16]; ll f(ll n) { ll left原创 2015-04-09 00:41:13 · 1021 阅读 · 0 评论 -
poj 2917 Diophantus of Alexandria 因数分解解1/x+1/y=1/n
题意: 给定n,求丢番图方程1/x+1/y=1/n(x 分析: 设x=n+a,y=n+b,化简可得n^2=a*b.设n^2的因子个数为p,n^2的所有因子中除n外都是成队出现的,故方程解数为(p+1)/2。 代码: //poj 2917 //sep9 #include using namespace std; int num[64]; int main() { int cases,原创 2015-06-26 10:03:09 · 1530 阅读 · 0 评论 -
poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用
题意: 给一个L,求长度最小的全8数满足该数是L的倍数。 分析: 转化为求方程a^x==1modm。之后就是各种数学论证了。 代码: //poj 3696 //sep9 #include #include using namespace std; typedef long long ll; ll L; ll factor[65536]; ll mul(ll x,ll y,ll p)原创 2015-06-04 17:43:21 · 1184 阅读 · 0 评论 -
poj 2480 Longge's problem 积性函数性质+欧拉函数
题意: 求f(n)=∑gcd(i, N) 1 分析: f(n)是积性的数论上有证明,且f(n)=sigma{1 代码: //poj 2480 //sep9 /* f(pi^ai) = Φ(pi^ai)+pi*Φ(pi^(ai-1))+pi^2*Φ(pi^(ai-2))+...+pi^(ai-1)* Φ(pi)+ pi^ai *Φ(1) = pi^(ai-1)*(pi-1) +原创 2015-06-03 00:11:13 · 1539 阅读 · 0 评论