鱼C论坛

 找回密码
 立即注册
查看: 902|回复: 10

[已解决]如图问题

[复制链接]
发表于 2023-9-24 12:06:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
屏幕截图 2023-09-24 120632.png
最佳答案
2023-9-24 12:24:31
本帖最后由 liuhongrun2022 于 2023-9-24 12:25 编辑

高精度乘法模板加循环也行(和第一个比简单很多)
  1. #include<iostream>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. using namespace std;  

  5. const int L=110;

  6. string mul(string a, string b)  
  7. {  
  8.     string s;  
  9.     int na[L], nb[L], nc[L], La = a.size(), Lb = b.size();
  10.     fill(na,na+L,0); fill(nb,nb+L,0); fill(nc,nc+L,0);
  11.     for(int i = La - 1; i >= 0; i--) na[La - i] = a[i] - '0';
  12.     for(int i = Lb - 1; i >= 0; i--) nb[Lb - i] = b[i] - '0';  
  13.     for(int i = 1; i <= La; i++)  
  14.         for(int j = 1; j <= Lb; j++)  
  15.             nc[i + j - 1] += na[i] * nb[j];
  16.     for(int i = 1; i <= La + Lb; i++)  
  17.         nc[i + 1] += nc[i] / 10, nc[i] %= 10;
  18.     if(nc[La + Lb]) s += nc[La + Lb] + '0';
  19.     for(int i = La + Lb - 1; i >= 1; i--)  
  20.         s += nc[i] + '0';
  21.     return s;  
  22. }  

  23. int main()  
  24. {  
  25.     string a;
  26.     int exponent;
  27.     while(cin >> a >> exponent)
  28.     {
  29.         string result = "1";
  30.         for (int i = 0; i < exponent; i++)
  31.         {
  32.             result = mul(result, a);
  33.         }
  34.         cout << result << endl;  
  35.     }
  36.     return 0;  
  37. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-9-24 12:07:53 | 显示全部楼层
成功屏蔽GPT
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-9-24 12:09:42 | 显示全部楼层
一定要c/c++吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-9-24 12:10:10 | 显示全部楼层

嗯……C++不行吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-9-24 12:11:44 | 显示全部楼层

想看看能不能实现
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-24 12:17:48 | 显示全部楼层
一个高精度幂的模板,输入两个数字,然后按回车。
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <map>
  7. #include <queue>
  8. #include <set>
  9. #include <vector>
  10. using namespace std;
  11. #define L(x) (1 << (x))
  12. const double PI = acos(-1.0);
  13. const int Maxn = 133015;
  14. double ax[Maxn], ay[Maxn], bx[Maxn], by[Maxn];
  15. char sa[Maxn/2],sb[Maxn/2];
  16. int sum[Maxn];
  17. int x1[Maxn],x2[Maxn];
  18. int revv(int x, int bits)
  19. {
  20.     int ret = 0;
  21.     for (int i = 0; i < bits; i++)
  22.     {
  23.         ret <<= 1;
  24.         ret |= x & 1;
  25.         x >>= 1;
  26.     }
  27.     return ret;
  28. }
  29. void fft(double * a, double * b, int n, bool rev)
  30. {
  31.     int bits = 0;
  32.     while (1 << bits < n) ++bits;
  33.     for (int i = 0; i < n; i++)
  34.     {
  35.         int j = revv(i, bits);
  36.         if (i < j)
  37.             swap(a[i], a[j]), swap(b[i], b[j]);
  38.     }
  39.     for (int len = 2; len <= n; len <<= 1)
  40.     {
  41.         int half = len >> 1;
  42.         double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len);
  43.         if (rev) wmy = -wmy;
  44.         for (int i = 0; i < n; i += len)
  45.         {
  46.             double wx = 1, wy = 0;
  47.             for (int j = 0; j < half; j++)
  48.             {
  49.                 double cx = a[i + j], cy = b[i + j];
  50.                 double dx = a[i + j + half], dy = b[i + j + half];
  51.                 double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx;
  52.                 a[i + j] = cx + ex, b[i + j] = cy + ey;
  53.                 a[i + j + half] = cx - ex, b[i + j + half] = cy - ey;
  54.                 double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx;
  55.                 wx = wnx, wy = wny;
  56.             }
  57.         }
  58.     }
  59.     if (rev)
  60.     {
  61.         for (int i = 0; i < n; i++)
  62.             a[i] /= n, b[i] /= n;
  63.     }
  64. }
  65. int solve(int a[],int na,int b[],int nb,int ans[])
  66. {
  67.     int len = max(na, nb), ln;
  68.     for(ln=0; L(ln)<len; ++ln);
  69.     len=L(++ln);
  70.     for (int i = 0; i < len ; ++i)
  71.     {
  72.         if (i >= na) ax[i] = 0, ay[i] =0;
  73.         else ax[i] = a[i], ay[i] = 0;
  74.     }
  75.     fft(ax, ay, len, 0);
  76.     for (int i = 0; i < len; ++i)
  77.     {
  78.         if (i >= nb) bx[i] = 0, by[i] = 0;
  79.         else bx[i] = b[i], by[i] = 0;
  80.     }
  81.     fft(bx, by, len, 0);
  82.     for (int i = 0; i < len; ++i)
  83.     {
  84.         double cx = ax[i] * bx[i] - ay[i] * by[i];
  85.         double cy = ax[i] * by[i] + ay[i] * bx[i];
  86.         ax[i] = cx, ay[i] = cy;
  87.     }
  88.     fft(ax, ay, len, 1);
  89.     for (int i = 0; i < len; ++i)
  90.         ans[i] = (int)(ax[i] + 0.5);
  91.     return len;
  92. }
  93. string mul(string sa,string sb)
  94. {
  95.     int l1,l2,l;
  96.     int i;
  97.     string ans;
  98.     memset(sum, 0, sizeof(sum));
  99.     l1 = sa.size();
  100.     l2 = sb.size();
  101.     for(i = 0; i < l1; i++)
  102.         x1[i] = sa[l1 - i - 1]-'0';
  103.     for(i = 0; i < l2; i++)
  104.         x2[i] = sb[l2-i-1]-'0';
  105.     l = solve(x1, l1, x2, l2, sum);
  106.     for(i = 0; i<l || sum[i] >= 10; i++)
  107.     {
  108.         sum[i + 1] += sum[i] / 10;
  109.         sum[i] %= 10;
  110.     }
  111.     l = i;
  112.     while(sum[l] <= 0 && l>0)
  113.     for(i = l; i >= 0; i--)    ans+=sum[i] + '0';
  114.     return ans;
  115. }
  116. string Pow(string a,int n)
  117. {
  118.     if(n==1) return a;
  119.     if(n&1) return mul(Pow(a,n-1),a);
  120.     string ans=Pow(a,n/2);
  121.     return mul(ans,ans);
  122. }
  123. int main()
  124. {
  125.     cin.sync_with_stdio(false);
  126.     string a;
  127.     int b;
  128.     while(cin>>a>>b) cout<<Pow(a,b)<<endl;
  129.     return 0;
  130. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-24 12:24:31 | 显示全部楼层    本楼为最佳答案   
本帖最后由 liuhongrun2022 于 2023-9-24 12:25 编辑

高精度乘法模板加循环也行(和第一个比简单很多)
  1. #include<iostream>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. using namespace std;  

  5. const int L=110;

  6. string mul(string a, string b)  
  7. {  
  8.     string s;  
  9.     int na[L], nb[L], nc[L], La = a.size(), Lb = b.size();
  10.     fill(na,na+L,0); fill(nb,nb+L,0); fill(nc,nc+L,0);
  11.     for(int i = La - 1; i >= 0; i--) na[La - i] = a[i] - '0';
  12.     for(int i = Lb - 1; i >= 0; i--) nb[Lb - i] = b[i] - '0';  
  13.     for(int i = 1; i <= La; i++)  
  14.         for(int j = 1; j <= Lb; j++)  
  15.             nc[i + j - 1] += na[i] * nb[j];
  16.     for(int i = 1; i <= La + Lb; i++)  
  17.         nc[i + 1] += nc[i] / 10, nc[i] %= 10;
  18.     if(nc[La + Lb]) s += nc[La + Lb] + '0';
  19.     for(int i = La + Lb - 1; i >= 1; i--)  
  20.         s += nc[i] + '0';
  21.     return s;  
  22. }  

  23. int main()  
  24. {  
  25.     string a;
  26.     int exponent;
  27.     while(cin >> a >> exponent)
  28.     {
  29.         string result = "1";
  30.         for (int i = 0; i < exponent; i++)
  31.         {
  32.             result = mul(result, a);
  33.         }
  34.         cout << result << endl;  
  35.     }
  36.     return 0;  
  37. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-24 12:27:09 | 显示全部楼层
另外,这两个例子都是C++的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-24 12:31:11 | 显示全部楼层
我要最佳答案!

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-24 14:42:04 | 显示全部楼层
  1. sh-5.1$ cat main.cpp
  2. #include <iostream>
  3. #include <boost/multiprecision/cpp_int.hpp>

  4. using std::cin, std::cout, std::endl;
  5. using boost::multiprecision::cpp_int;
  6. using boost::multiprecision::pow;

  7. int main() {
  8.     cout << pow(cpp_int(2), 100) << endl;
  9.     return 0;
  10. }
  11. sh-5.1$ ./main
  12. 1267650600228229401496703205376
  13. sh-5.1$
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-24 21:06:43 | 显示全部楼层
isdkz用的机器人好像可以看图片
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 16:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表