2013 ACM-ICPC长沙赛区全国邀请赛So Easy! && 2015 ACM/ICPC Asia Regional Shenyang Online-Best Solver

<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! 
 

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 (0x<232)  and a given prime number  M (M46337) , print  [y]%M . ( [y]  means the integer part of  y )
 

Input
An integer  T (1<T1000) , indicating there are  T  test cases.
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 .
 

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
        
        
       
       
      
      
     
     
    
    


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值