想要最小化我的代码,以便它消耗的时间少于 1 秒它使用模幂的概念正确输出但超出时间限制

如何解决想要最小化我的代码,以便它消耗的时间少于 1 秒它使用模幂的概念正确输出但超出时间限制

下面的代码是计算 2^n,其中 n 等于 1 。所以为了计算这么大的数字,我使用了模指数的概念。代码给出了正确的输出,但由于大量的测试用例,它超出了时间限制。我没有找到最小化解决方案的方法,因此它消耗的时间更少。由于“算法”函数被调用的次数与测试用例的数量一样多。所以我想把“algo”函数中使用的逻辑放在 main() 函数中,这样它消耗的时间少于 1 秒,并给出正确的输出。这里“t”代表测试用例的数量,它的值为1 。

你方的任何建议都会有很大帮助!!

#include<iostream>
#include<math.h>

using namespace std;

int algo(int x,int y){
  long m = 1000000007;
  if(y == 0){
    return 1;
  }

  int k = algo(x,y/2);

  if (y % 2 == 1){
    return ((((1ll * k * k) % m) * x) % m);
  } else if (y % 2 == 0){
    return ((1ll * k * k) % m);
  }
  
}

int main(void)
{
    int n,t,k;
    cin>>t; //t = number of test cases
    for ( k = 0; k < t; k++)
    {
        cin >> n;  //power of 2 

        cout<<"the value after algo is: "<<algo(2,n)<<endl;

    }
    return 0;
}

解决方法

你可以利用二进制移位来求二的幂

#include <iostream>
using namespace std;
int main()
{
    unsigned long long u = 1,w = 2,n = 10,p = 1000000007,r;
    //n -> power of two 
    while (n != 0)
    {
        if ((n & 0x1) != 0)
            u = (u * w) % p;

        if ((n >>= 1) != 0)
            w = (w * w) % p;
    }
    r = (unsigned long)u;
    cout << r;
    return 0;
}
,

这是我经常用来计算的函数
任何整数 X 乘以 Y 模 M

计算 (X^Y) mod M 的 C++ 函数

int power(int x,int y,const int mod = 1e9+7)
{
    int result = 1;
    x = x % mod;
    if (x == 0)
        return 0;
    while (y > 0)
    {
        if (y & 1)
            result = ( (result % mod) * (x % mod) ) % mod;
        y = y >> 1; // y = y / 2
        x = ( (x % mod) * (x % mod) ) % mod;
    }
    return result;
}
  • 如果您不想要,请移除 Mod。
  • 此函数的时间复杂度为 O(log2(Y))
  • 可能会出现溢出的情况,因此请根据需要使用 int,long,long long 等。
,

好吧,你的变量不会支持边界测试用例,引入 2^10000,1 RIP 算法

1995063116880758384883742162683585083823496831886192454852008949852943883022194663191996168403619459789933112942320912427155649134941378111759378593209632395785573004679379452676524655126605989552055008691819331154250860846061810468550907486608962488809048989483800925394163325785062156830947390255691238806522509664387444104675987162698545322286853816169431577562964076283688076073222853509164147618395638145896946389941084096053626782106462142733339403652556564953060314268023496940033593431665145929777327966577560617258203140799419817960737824568376228003730288548725190083446458145465055792960141483392161573458813925709537976911927780082695773567444412306201875783632550272832378927071037380286639303142813324140162419567169057406141965434232463880124885614730520743199225961179625013099286024170834080760593232016126849228849625584131284406153673895148711425631511108974551420331382020293164095759646475601040584584156607204496286701651506192063100418642227590867090057460641785695191145605 5068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241 0374914109667185570507590981002467898801782719259533812824219540283027594084489550146766683896979968862416363133763939033734558014076367418777110553842257394991101864682196965816514851304942223699477147630691554682176828762003627772577237813653316111968112807926694818872012986436607685516398605346022978715575179473852463694469230878942659482170080511203223654962881690357391213683383935917564187338505109702716139154395909915981546544173363116569360311222499379699992267817323580231118626445752991357581750081998392362846152498810889602322443621737716180863570154684840586223297928538756234865564405369626220189635710288123615675125433383032700290976686505685571575055167275188991941297113376901499161813151715440077286505731895574509203301853048471138183154073240533190384620840364217637039115506397890007428536721962809034779745333204683687958685802379522186291200807428195513179481576244482985184615097048880272747215746881315947504097321150804981904558034168269497871413160632106863915116817743 04792596709376

不要害怕我的朋友,有人试图解决问题https://www.quora.com/What-is-2-raised-to-the-power-of-50-000,您正在寻找 Piyush Michael 的答案,这是他的示例代码

#include <stdio.h> 
int main() 
{ 
 int ul=16,000; 
 int rs=50,000; 
 int s=0,carry[ul],i,j,k,ar[ul]; 
 ar[0]=2; 
 for(i=1;i<ul;i++)ar[i]=0; 
 for(j=1;j<rs;j++) 
 {for(k=0;k<ul;k++)carry[k]=0; 
  for(i=0;i<ul;i++) 
  {ar[i]=ar[i]*2+carry[i]; 
   if(ar[i]>9) 
   {carry[i+1]=ar[i]/10; 
    ar[i]=ar[i]%10; 
   } 
  } 
 } 
 for(j=ul-1;j>=0;j--)printf("%d",ar[j]); 
 for(i=0;i<ul-1;i++)s+=ar[i]; 
 printf("\n\n%d",s); 
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res