突然发现初中数竞的数论已经撑不起走了。。:)
exgcd(不要查我水表)
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面
#include<iostream> #include<cstdio> #include<cmath> using namespace std; long long x, y, m, n, L; long long k, t, s; void exgcd(long long a, long long b, long long &d, long long &x, long long &y) { if(b == 0) { d = a, x = 1, y = 0; } else { long long x0, y0; exgcd(b, a % b, d, x0, y0); x = y0; y = x0 - (a / b) * y0; } } int main ( ) { scanf ( "%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L ); exgcd ( n - m, L, s, t, k); if ( (x - y) % s ) { printf ( "Impossible" ); return 0; } t = t * ((x - y)/s); t = (t % (L / s) + (L / s) ) % (L / s); printf ( "%lld", t ); return 0; }
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int mod=10000; int n; struct node { int w[2][2]; }; node a,b; node che(node a,node b) { node c; for (int i=0;i<=1;i++) for (int j=0;j<=1;j++) c.w[i][j]=(a.w[i][0]*b.w[0][j]%mod+a.w[i][1]*b.w[1][j]%mod)%mod; return c; } node cheng(int n) { if (n==1) return a; node b=cheng(n/2); node c=che(b,b); if (n%2==1) c=che(c,a); return c; } int main() { while (scanf("%d",&n)&&n!=-1){ a.w[0][0]=1; a.w[0][1]=1; a.w[1][0]=1; a.w[1][1]=0; if (n==0) printf("0\n"); else if (n==1) printf("1\n"); else if (n==2) printf("1\n"); else { node x=cheng(n-1); printf("%d\n",x.w[0][0]%mod); } } return 0; }真的很开心呢