题目:
Read the program below carefully then answer the question.
#pragma comment(linker, “/STACK:1024000000,1024000000”)
#include
#include
#include
#include
#include
#include
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans2+1)%m;
else ans=ans2%m;
}
printf("%d\n",ans);
}
return 0;
}
Input
Multi test cases,each line will contain two integers n and m. Process to end of file.
[Technical Specification]
1<=n, m <= 1000000000
Output
For each case,output an integer,represents the output of above program.
Sample Input
1 10
3 100
Sample Output
1
5
题意:
写出一个输入输出符合已知代码的新代码。
有递推式:
A 0 = 0 A_{0}=0 A0=0
A n = 2 A n − 1 + 1 A_{n}=2A_{n-1}+1 An=2An−1+1,n为奇数----n>=1
A n = 2 A n − 1 A_{n}=2A_{n-1} An=2An−1,n为偶数----n>=2
现输入n和m,求 A n A_{n} An对m取模后的值
显然不能得到相邻两项的一个递推式,但是间隔一项的递推关系是可以得出的,也就是相邻三项的关系。
A 2 k + 1 = 2 A 2 k + 1 = 2 ( 2 A 2 k − 1 ) + 1 = 4 A 2 k − 1 + 1 A_{2k+1}=2A_{2k}+1=2(2A_{2k-1})+1 = 4A_{2k-1}+1 A2k+1=2A2k+1=2(2A2k−1)+1=4A2k−1+1 ①
= A 2 k + A 2 k + 1 = A 2 k + 2 A 2 k − 1 + 1 = A_{2k}+A_{2k}+1 = A_{2k}+2A_{2k-1}+1 =A2k+A2k+1=A2k+2A2k−1+1 ②
由②式,我们容易得到 A n = A n − 1 + 2 A n − 2 + 1 A_{n}=A_{n-1}+2A_{n-2}+1 An=An−1+2An−2+1
于是构造变换矩阵M:
∣ 0 2 0 1 1 0 0 1 1 ∣ \begin{vmatrix} 0 & 2 &0\\ 1 & 1 & 0\\ 0 & 1& 1 \end{vmatrix} ∣∣∣∣∣∣010211001∣∣∣∣∣∣
那么可得:
∣ A n − 2 A n − 1 1 ∣ \begin{vmatrix} A_{n-2} & A_{n-1} & 1 \end{vmatrix} ∣∣An−2An−11∣∣ · ∣ 0 2 0 1 1 0 0 1 1 ∣ = ∣ A n − 1 A n 1 ∣ \begin{vmatrix} 0 & 2 &0\\ 1 & 1 & 0\\ 0 & 1& 1 \end{vmatrix}=\begin{vmatrix} A_{n-1} & A_{n} & 1 \end{vmatrix} ∣∣∣∣∣∣010211001∣∣∣∣∣∣=∣∣An−1An1∣∣
因此我们利用快速幂模板求出Mn-2,再乘上矩阵 ∣ A 1 A 2 1 ∣ \begin{vmatrix} A_{1} & A_{2} & 1 \end{vmatrix} ∣∣A1A21∣∣ 即可
代码:
#include
#include
#include
#define ll long long
using namespace std;
ll n,m;
ll rec[3]={1,2,1};
ll E[3][3]={{1,0,0},{0,1,0},{0,0,1}};
void mul(ll a[3][3],ll b[3][3])
{
ll c[3][3];
memset(c,0,sizeof(c));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
c[i][j]=(c[i][j]+(a[i][k]*b[k][j])%m+m)%m;//这里+m可省,但当递推式出现负系数时不可。
memcpy(a,c,sizeof(c));
}
void pow(ll b)
{
ll M[3][3]={{0,2,0},{1,1,0},{0,1,1}};
while(b)
{
if(b&1) mul(E,M);
mul(M,M);
b>>=1;
}
}
int main()
{
while(~scanf("%lld%lld",&n,&m))
{
ll ans=0;
if(n==1)
{
cout<<1%m<<endl;
continue;
}
if(n==2)
{
cout<<2%m<<endl;
continue;
}
memset(E,0,sizeof(E));//初始化单位阵不能忘
for(int i=0;i<3;i++) E[i][i]=1;
pow(n-2);
for(int i=0;i<3;i++)
ans=(ans+rec[i]*E[i][1]%m)%m;
printf("%lld\n",ans);
}
}
对于①式:
A 2 k + 1 = 4 A 2 k − 1 + 1 ( k > = 1 ) , A 1 = 1 A_{2k+1} = 4A_{2k-1}+1 (k>=1),A_{1}=1 A2k+1=4A2k−1+1(k>=1),A1=1。
得到 A 2 k − 1 = 1 + 4 + 4 ² + . . . + 4 k − 1 A_{2k-1}=1+4+4²+...+4^{k-1} A2k−1=1+4+4²+...+4k−1
而当n是偶数时,我们将 A n − 1 A_{n-1} An−1乘2即可
我们可以从数列求和的角度考虑
递归求等比数列模板
ll cal(int p,int n){ ///这里是递归求解等比数列模板 1+p+p^2...+p^n
if(n==0) return 1;
if(n&1){//(1+p+p^2+....+p^(n/2))*(1+p^(n/2+1));
return (1+quick_pow(p,n/2+1))*cal(p,n/2)%mod;
}
else { //(1+p+p^2+....+p^(n/2-1))*(1+p^(n/2+1))+p^(n/2);
return (quick_pow(p,n/2)+(1+quick_pow(p,n/2+1))*cal(p,n/2-1))%mod;
}
}
代码:
#include
#include
#include
#include
#include
#include
typedef long long LL;
LL mod;
LL quick_pow(LL a,LL n){
LL ans = 1;
while(n){
if(n&1) ans=ans*a%mod;
a= a*a%mod;
n>>=1;
}
return ans;
}
LL cal(int p,int n){ ///这里是递归求解等比数列模板 1+p+p^2...+p^n
if(n==0) return 1;
if(n&1){//(1+p+p^2+....+p^(n/2))*(1+p^(n/2+1));
return (1+quick_pow(p,n/2+1))*cal(p,n/2)%mod;
}
else { //(1+p+p^2+....+p^(n/2-1))*(1+p^(n/2+1))+p^(n/2);
return (quick_pow(p,n/2)+(1+quick_pow(p,n/2+1))*cal(p,n/2-1))%mod;
}
}
int main()
{
LL n;
while(scanf("%lld%lld",&n,&mod)!=EOF)
{
if(n==1&&mod==1) {
printf("0\n");
continue;
}
LL k = (n+1)/2;
LL ans = cal(4,k-1);
if(n&1){
printf("%lld\n",ans);
}else {
printf("%lld\n",ans*2%mod);
}
}
return 0;
}
当然也可以利用等比数列求和公式
A 2 k − 1 = 1 + 4 + 4 ² + . . . + 4 k − 1 A_{2k-1}=1+4+4²+...+4^{k-1} A2k−1=1+4+4²+...+4k−1
A 2 k − 1 = 4 k − 1 3 A_{2k-1}=\frac{4^k-1}{3} A2k−1=34k−1,用 k = n + 1 2 k=\frac{n+1}{2} k=2n+1 代入得到 A n = 2 n + 1 − 1 3 A_n=\frac{2^{n+1}-1}{3} An=32n+1−1(n为奇数)
但是对于除数求余,考虑如下性质:
代码:
#include
using namespace std;
long long n, m;
long long fast_pow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % m;
a = a * a % m;
b >>= 1;
}
return ans;
}
int main() {
while (scanf("%lld%lld", &n, &m) != EOF) {
long long last = m;
m = m * 3;///
long long ans = 0;
if (n & 1) {
ans = (fast_pow(2, n + 1) - 1) % m / 3;
}
else {
ans = (fast_pow(2, n) - 1) % m / 3;
ans = ans * 2 % last;
}
cout << ans << endl;
}
return 0;
}