Interesting Integers(CF---BAPC 14 + hnoj11589)扩展欧几里得
来源:互联网 发布:工业网络交换机 编辑:程序博客网 时间:2024/06/06 15:41
题意:类斐波那契;G[k]=G[k-1]+G[k-2];(k>2);使得初始值G[1]和G[2]尽量小(且G[1]<=G[2])
思路:G[3]=1*G[1]+1*G[2];
G[4]=1*G[1]+2*G[2];
G[5]=2*G[1]+3*G[2];
G[6]=3*G[1]+5*G[2];
............... //系数满足fib函数1 1 2 3 5 8 13.......
G[k]=fib[k-2]*G[1]+fib[k-1]*G[2];
.......so.由扩展欧几里得求同余方程 a=fib[k-2]、b=fib[k-1];
ax+by=c ( x=G[1],y=G[2],c=G[k])
然后注意优化下,求得的最小正整数x,使得y尽量小但是满足y>=x;
详见代码;
转载请注明出处:寻找&星空の孩子
题目链接:http://codeforces.com/gym/100526/attachments 或者 http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11589
用__int64交,long long 会WA
#include<stdio.h>#define ll __int64#define INF 0x7FFFFFFFll fib[55],len;//用__int64void init(){ fib[0]=0; fib[1]=fib[2]=1; ll i; for(i=3;; i++) { fib[i]=fib[i-1]+fib[i-2]; if(fib[i]>1000000000) break; } len=i; // for(i=1;i<len;i++) //printf("%lld %lld\n",i,fib[i]);}void exgcd(ll a,ll b,ll &d,ll &x,ll &y){ if(!b) d=a,x=1,y=0; else { exgcd(b,a%b,d,y,x); y-=x*(a/b); }}ll xx,yy;int main(){ int T; scanf("%d",&T); init(); // printf("len=%lld\n",len); while(T--) { ll c,a,b,x,y,d; xx=INF,yy=INF; scanf("%I64d",&c); for(ll i=2; i<len; i++) { a=fib[i-1]; b=fib[i]; exgcd(a,b,d,x,y); // printf("x=%lld,y=%lld\n",x,y); if(c%d) continue; else { x=x*(c/d); y=y*(c/d); ll ma=b/d; ll mb=a/d; x=(x%ma+ma)%ma; y=(c-x*a)/b; ll k = (y-x-1) / (ma+mb);//优化 x += ma*k, y -= mb*k; while (y >= x && y <= yy) { if ((y < yy || (y == yy && x < xx))&&x>0) xx = x, yy = y; x += ma, y -= mb; } } } printf("%I64d %I64d\n",xx,yy); } return 0;}
0 0
- Interesting Integers(CF---BAPC 14 + hnoj11589)扩展欧几里得
- gym 101512 BAPC 2014 I Interesting Integers (拓展欧几里得 + 斐波那契)
- GYM100526 Interesting Integers(扩展欧几里得)
- gym 101512 BAPC 2014 I Interesting Integers
- 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)
- cf 724CRay Tracing(扩展欧几里得)
- 【POJ 2891】Strange Way to Express Integers(扩展欧几里得)
- POJ 2891 Strange Way to Express Integers (扩展欧几里得)
- Strange Way to Express Integers 扩展欧几里得
- 欧几里得+扩展欧几里得(理解)
- Strange Way to Express Integers (模线性方程组(扩展欧几里得))
- CF 483D - Interesting Array(TODO)
- 扩展欧几里得算法,中国剩余定理(Two Arithmetic Progressions,cf 710D)
- poj 2891 Strange Way to Express Integers 扩展欧几里得运用
- POJ Strange Way to Express Integers 2891(扩展欧几里得)
- POJ-----2891---Strange Way to Express Integers扩展欧几里得
- POJ 2891 Strange Way to Express Integers(扩展欧几里得)
- sgu106(扩展欧几里得)
- 【杂题】CodeforcesRound #315 (Div. 2)569C Primes or Palindromes?
- makefile中的patsubst
- 结构体字节对齐
- redo和undo的区别
- spring版本区别
- Interesting Integers(CF---BAPC 14 + hnoj11589)扩展欧几里得
- twisted.http 异步访问站点
- 事件模型
- 红杏: 为学者、程序员、外贸工作者打造的上网加速器,专治各种网站打不开和打开慢,而且,更棒的是,红杏按需生效,不影响普通网站的访问速度。 杏不杏?由你红杏: 为学者、程序员、外贸工作者打造的上网加速器
- Bit Manipulation - Power of Two
- hdu2037
- 图的点连通度边连通度总结
- prim算法模版 hdu 1875
- UI05 _ 分段控件 -- UISegmentedControl