• 5.24 基础题目


     

    快速幂

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. //126348976 982638476 938420413
    4. int main(){
    5. int a,b,p;
    6. cin>>a>>b>>p;
    7. long long res = 1,ci=1;
    8. int flag=0;
    9. if(b==0){
    10. res%=p;
    11. }
    12. else{
    13. while(b){
    14. if (flag==0)ci=a%p;
    15. else
    16. ci=(ci%p)*(ci%p)%p;
    17. if (b&1)res=(res*(ci%p))%p;
    18. b>>=1;
    19. flag++;
    20. //cout<<ci<<" "<<res<<endl;
    21. }
    22. }
    23. cout<<res<<endl;
    24. return 0;
    25. }

    64位整数乘法

    1. #include<bits/stdc++.h>
    2. /*
    3. a*b=a+a+a+...+a
    4. a*1=a
    5. a*2=2a
    6. a*4=4a;
    7. ...
    8. a*(2^k)=2^k*a;
    9. */
    10. using namespace std;
    11. typedef long long LL;
    12. int main(){
    13. LL a,b,p;
    14. cin>>a>>b>>p;
    15. LL res=0;
    16. while(b){
    17. if(b&1)res=(res+a)%p;
    18. b>>=1;
    19. a=a*2%p;
    20. }
    21. cout<<res<<endl;
    22. return 0;
    23. }

    最短Hamilton路径

    1s约计算1亿次;

     f[state][j]=

    f[state_k][j]+weight[k][j];

    state_k是state除去j之后的集合,state_k要包含k,k是state_k二进制表示中为1的下标,枚举出k

    state_k=state-2^j;

    for (int j = 0;j < 点数;j++){//n点数

            if ((state_k>>j )& 1 == 0)

              {    

                    state=state_k+2^j;

                    f[state][j]=f[state_k][j]+weight[k][j];

              }

    }

     

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. const int N=20,M=1<<20;
    4. int n;
    5. int f[M][N],weight[N][N];
    6. int main(){
    7. cin>>n;
    8. for(int i = 0;i < n;i++)
    9. for(int j = 0;j < n;j++){
    10. cin>>weight[i][j];
    11. }
    12. memset(f,0x3f,sizeof(f));
    13. f[1][0]=0;
    14. for(int i = 0;i < 1<<n;i++)//枚举所有的状态
    15. for(int j = 0;j < n;j++){//枚举状态i二进制表示中所有的1的位置
    16. if(i>>j & 1)
    17. for(int k = 0;k < n;k++)//枚举状态i去掉第j位后的剩余的1的位置
    18. if(i-(1<<j) >> k & 1)//第k位是1
    19. f[i][j]=min(f[i][j],f[i-(1<<j)][k]+weight[k][j]);
    20. }
    21. cout<<f[(1<<n)-1][n-1]<<endl;
    22. return 0;
    23. }

    汉诺塔问题(三塔)

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. //f(x)=2^x-1
    4. int hnt(int st,int mid,int dst,int n){
    5. if(n==1){
    6. //cout<<"from "<<char(st)<<" through "<<char(mid)<<" to "<<char(dst)<<" with "<<n<<endl;
    7. return 1;
    8. }
    9. else {
    10. int sum=0;
    11. cout<<"from "<<char(st)<<" through "<<char(dst)<<" to "<<char(mid)<<" with "<<n-1<<endl;
    12. sum+=hnt(st,dst,mid,n-1);
    13. cout<<"from "<<char(st)<<" through "<<char(mid)<<" to "<<char(dst)<<" with "<<1<<endl;
    14. sum+=hnt(st,mid,dst,1);
    15. cout<<"from "<<char(mid)<<" through "<<char(st)<<" to "<<char(dst)<<" with "<<n-1<<endl;
    16. sum+=hnt(mid,st,dst,n-1);
    17. return sum;
    18. }
    19. }
    20. int main(){
    21. int st,mid,dst,n;
    22. st=int('A'),mid=int('B'),dst=int('C');
    23. n=12;
    24. int ans = hnt(st,mid,dst,n);
    25. cout<<ans;
    26. return 0;
    27. }

    四塔汉诺塔问题

    // 凡是用到min的都需要,赋较大值。
        // memset以字节形式重置(int: 0x3f3f3f3f)
        //又0x3f的2倍为最大整数,所以还可以满足加法不越界

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N=15;
    4. int d[N],f[N];
    5. int main(){
    6. d[1]=1;
    7. for(int i = 2;i <=12;i++ )
    8. d[i]=d[i-1]+1+d[i-1];
    9. memset(f,0x3f,sizeof(f));
    10. f[0]=0;
    11. f[1]=1;
    12. //先让i个盘到B塔或者C塔,剩下的n-i盘在3塔情况下移到D
    13. //再将i盘在4塔(因为D塔都是更重的盘)情况下移动到D。因为对于i我们不知道哪个最优,因此
    14. //因此推导为 f[n] = min(f[n],2*f[i]+d[n-i])(for i in [1..n-1])
    15. for(int i =2;i <= 12;i++)
    16. for(int j = 1;j < i;j++)
    17. f[i]=min(f[i],f[j]+d[i-j]+f[j]);
    18. for(int i =1;i <= 12;i++)
    19. cout<<f[i]<<endl;
    20. return 0;
    21. }

  • 相关阅读:
    礼物道具投票系统源码 可以无限多开 吸粉神器 附带完整的搭建教程
    IP真人识别方法与代理IP检测技术
    8.Ribbon负载均衡服务调用
    Undefined reference to pthread_create in Linux
    浏览器输入URL后到服务器返回数据大体过程
    linux-ARM下的数据库管理工具的安装使用(dbeaver)
    浅谈电商会员管理的价值|数商云B2B系统助力家用电器行业提高交易转化
    HTML 样式-CSS
    在字节跳动干软件测试5年,4月无情被辞,想给划水的兄弟提个醒
    @Accessors 注解详解
  • 原文地址:https://blog.csdn.net/weixin_54010759/article/details/130854401