线性同余方程组的合并



a_ix\equiv b_i\mod m_i

x\equiv B \mod lcm(m_i)

a_1x \equiv b_1 \mod m1

x\equiv (b_1/\gcd(a_1,m_1))*(a_1^{-1}/gcd(a_1,m_1)) \mod (m_1/gcd(a_1,m_1))

x \equiv B_1 \mod m1

x=B_1+m_1*t 此时的m1*t是未知的

a(B_1+m_1t) \equiv b_2 \mod m_2

am_1t \equiv b_2-aB_1 \mod m_2

t \equiv (b_2-aB_1)/gcd(am_1,m_2) * (am_1)^{-1}/gcd(am_1,m_2) \mod (m_2)/gcd(am_1,m_2)

x=B_1+m_1*t

$此时的x在\mod m1和\mod m2的意义下都是成立的。B1_+m_1*t变成了新的B_2$

x\equiv B_2 \mod lcm(m_1,m_2)

x=B_2+lcm*t

//返回一个b，m对
pair<int, int> inner_congruence(const vector<int> &A,const vector<int> &B,const vector<int> &M)
{
int x=0,m=1;
for(int i-9;i<A.size();i++)
{
int a=A[i]*m,b=B[i]-A[i]*x,d=gcd(a,M[i]);
if(b%d!=0) return make_pair(0,-1); 

【矩阵快速幂】nth Term LightOJ – 1096

You have to find the nth term of the following function:

f(n) = a * f(n-1) + b * f(n-3) + c, if(n > 2)

= 0, if(n ≤ 2)

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains four integers n (0 ≤ n ≤ 108), a b c (1 ≤ a, b, c ≤ 10000).

Output
For each case, print the case number and f(n) modulo 10007.

Sample Input


【数学-贪心-末状态已知】问题集合

• 蓝书 P4 分金币

https://vjudge.net/problem/UVA-11300

• 蓝书 P7 雕塑

A-X_1+X_2=M =X_2=X_1-(A-M)

A-X_2+X_3=M =X_3=X_1-2(A-M)

A-X_n+X_1=M =X_n=X_1-n(A-M)