zoukankan      html  css  js  c++  java
  • [题解]RQNOJ PID85 三个袋子

    链接:http://www.rqnoj.cn/problem/85

    思路:一个排列问题,递推式很简单,f(n+1)=3*f(n)-1 ,由此可以推出通项公式,f(n)=0.5*3^(n-1)+0.5 。

            但是这个数太大了,我们需要求的是f(n) mod K 。那么就必须考虑同余的性质。

            我们知道2m≡a mod k 并不能推出m≡a/2 mod k ,但是2m≡a mod 2*k 却可以推出m≡a/2 mod k 。借助此性质再加上一个快速幂即可得出答案。

    我的实现:

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 int N,K;
     5 int ans;
     6 int Pow(int Num,int P,int MOD)//(Num^P)%MOD
     7 {
     8     if(Num<=1||P==1)
     9         return Num%MOD;
    10     if(P==0)
    11         return 1;
    12     int Ret=Pow(Num,P/2,MOD);
    13     Ret=(1LL*Ret*Ret)%MOD;
    14     if(P&1)
    15         Ret=(1LL*Ret*Num)%MOD;
    16     return Ret;
    17 }
    18 int main()
    19 {
    20     scanf("%d%d",&N,&K);
    21     ans=(Pow(3%(K*2),N-1,K*2)+1)%(K*2);
    22     ans/=2;
    23     printf("%d
    ",ans);
    24     return 0;
    25 }
    View Code

    PS.zyy数学太渣了,大脑没有储存关于同余的东东,于是被这道题卡了好久好久…… 话说网上还有一种利用循环节快速找到答案的方法,zyy智商不够没看懂,求各位大神指点指点~~~

  • 相关阅读:
    mysql数据库 详解
    0810 smarty
    抽象类
    Nginx 负载均衡策略
    Nginx 负载均衡配置和策略
    内置Web Server
    PHP运行方式
    MySQL create table 语法
    MySQL 索引的使用
    MySQL的 explain 解析
  • 原文地址:https://www.cnblogs.com/CQBZOIer-zyy/p/3826391.html
Copyright © 2011-2022 走看看