<span style="background-color: transparent; color: rgb(124, 169, 237); font-family: Arial; font-size: 18px;">So Easy!</span>
<span style="background-color: transparent; color: rgb(124, 169, 237); font-family: Arial; font-size: 18px;">So Easy!</span>
A sequence S
n is defined as:
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S n.
You, a top coder, say: So easy!
Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S n.
You, a top coder, say: So easy!
Input
There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 2
15, (a-1)
2< b < a
2, 0 < b, n < 2
31.The input will finish with the end of file.
Output
For each the case, output an integer S
n.
Sample Input
2 3 1 2013 2 3 2 2013 2 2 1 2013
Sample Output
4 14 4
4
那么用矩阵快速幂就可以了,代码如下:
#include
using namespace std;
__int64 A[3][3],s[3][3],tmp[3][3];
void fun(__int64 n,__int64 m)
{
__int64 i,j,k;
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
{
s[i][j]=A[i][j];
}
--n;//本身存的A就是已经是一次的了,所以要少乘一次
while(n)
{
if(n&1)
{
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
{
tmp[i][j]=A[i][j];
A[i][j]=0;
}
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
for(k=1;k<=2;k++)
{
A[i][j]=((A[i][j]+tmp[i][k]*s[k][j])%m+m)%m;
}
}
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
{
tmp[i][j]=s[i][j];
s[i][j]=0;
}
for(i=1;i<=2;i++)
for(j=1;j<=2;j++)
for(k=1;k<=2;k++)
{
s[i][j]=((s[i][j]+tmp[i][k]*tmp[k][j])%m+m)%m;
}
n>>=1;
}
}
int main()
{
__int64 a,b,n,m;
__int64 i,j;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&m)==4)
{
if(n==1)
{
printf("%I64d\n",2*a%m);
continue;
}
A[1][1]=2*a; A[1][2]=b-a*a; A[2][1]=1; A[2][2]=0;
fun(n-1,m);//传入n-1是因为递推矩阵的幂为n-1时计算的才是Sn
printf("%I64d\n",((A[1][1]*2*a+A[1][2]*2)%m+m)%m);
}
return 0;
}
Best Solver
Problem Description
The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart.
It is known that y=(5+26√)1+2x .
For a given integer x (0≤x<232) and a given prime number M (M≤46337) , print [y]%M . ( [y] means the integer part of y )
It is known that y=(5+26√)1+2x .
For a given integer x (0≤x<232) and a given prime number M (M≤46337) , print [y]%M . ( [y] means the integer part of y )
Input
An integer
T (1<T≤1000)
, indicating there are
T
test cases.
Following are T lines, each containing two integers x and M , as introduced above.
Following are T lines, each containing two integers x and M , as introduced above.
Output
The output contains exactly
T
lines.
Each line contains an integer representing [y]%M .
Each line contains an integer representing [y]%M .
Sample Input
7 0 46337 1 46337 3 46337 1 46337 21 46337 321 46337 4321 46337
Sample Output
Case #1: 97 Case #2: 969 Case #3: 16537 Case #4: 969 Case #5: 40453 Case #6: 10211 Case #7: 17947
此题的a、b完全满足上题的要求,可用与上题完全相同的思路求解,唯一的不同点即为时向下取整而非向上取整,那么,在上题的答案基础上,减一,就是此题的答案。
还有就是指数太大,要找到循环节,其实由于mod小,循环节并没有太大,是可以搞的。
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const LL maxn = 47000;
LL r[maxn], ans[maxn], x, m;
LL Pow (LL a, LL n, LL mod)
{
LL res = 1;
while (n)
{
if (n % 2)
res = (res * a) % mod;
a =(a * a) % mod;
n /= 2;
}
return res;
}
void init ()
{
ans[0] = 2;
ans[1] = 10;
for (int i=2; i