鱼C论坛

 找回密码
 立即注册
查看: 1222|回复: 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 编辑

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

const int L=110;

string mul(string a, string b)  
{  
    string s;  
    int na[L], nb[L], nc[L], La = a.size(), Lb = b.size();
    fill(na,na+L,0); fill(nb,nb+L,0); fill(nc,nc+L,0); 
    for(int i = La - 1; i >= 0; i--) na[La - i] = a[i] - '0';
    for(int i = Lb - 1; i >= 0; i--) nb[Lb - i] = b[i] - '0';  
    for(int i = 1; i <= La; i++)  
        for(int j = 1; j <= Lb; j++)  
            nc[i + j - 1] += na[i] * nb[j];
    for(int i = 1; i <= La + Lb; i++)  
        nc[i + 1] += nc[i] / 10, nc[i] %= 10;
    if(nc[La + Lb]) s += nc[La + Lb] + '0'; 
    for(int i = La + Lb - 1; i >= 1; i--)  
        s += nc[i] + '0'; 
    return s;  
}  

int main()  
{  
    string a;
    int exponent;
    while(cin >> a >> exponent)
    {
        string result = "1";
        for (int i = 0; i < exponent; i++)
        {
            result = mul(result, a);
        }
        cout << result << endl;  
    }
    return 0;  
}
想知道小甲鱼最近在做啥?请访问 -> 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 | 显示全部楼层
一个高精度幂的模板,输入两个数字,然后按回车。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define L(x) (1 << (x))
const double PI = acos(-1.0);
const int Maxn = 133015;
double ax[Maxn], ay[Maxn], bx[Maxn], by[Maxn];
char sa[Maxn/2],sb[Maxn/2];
int sum[Maxn];
int x1[Maxn],x2[Maxn];
int revv(int x, int bits)
{
    int ret = 0;
    for (int i = 0; i < bits; i++)
    {
        ret <<= 1;
        ret |= x & 1;
        x >>= 1;
    }
    return ret;
}
void fft(double * a, double * b, int n, bool rev)
{
    int bits = 0;
    while (1 << bits < n) ++bits;
    for (int i = 0; i < n; i++)
    {
        int j = revv(i, bits);
        if (i < j)
            swap(a[i], a[j]), swap(b[i], b[j]);
    }
    for (int len = 2; len <= n; len <<= 1)
    {
        int half = len >> 1;
        double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len);
        if (rev) wmy = -wmy;
        for (int i = 0; i < n; i += len)
        {
            double wx = 1, wy = 0;
            for (int j = 0; j < half; j++)
            {
                double cx = a[i + j], cy = b[i + j];
                double dx = a[i + j + half], dy = b[i + j + half];
                double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx;
                a[i + j] = cx + ex, b[i + j] = cy + ey;
                a[i + j + half] = cx - ex, b[i + j + half] = cy - ey;
                double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx;
                wx = wnx, wy = wny;
            }
        }
    }
    if (rev)
    {
        for (int i = 0; i < n; i++)
            a[i] /= n, b[i] /= n;
    }
}
int solve(int a[],int na,int b[],int nb,int ans[])
{
    int len = max(na, nb), ln;
    for(ln=0; L(ln)<len; ++ln);
    len=L(++ln);
    for (int i = 0; i < len ; ++i)
    {
        if (i >= na) ax[i] = 0, ay[i] =0;
        else ax[i] = a[i], ay[i] = 0;
    }
    fft(ax, ay, len, 0);
    for (int i = 0; i < len; ++i)
    {
        if (i >= nb) bx[i] = 0, by[i] = 0;
        else bx[i] = b[i], by[i] = 0;
    }
    fft(bx, by, len, 0);
    for (int i = 0; i < len; ++i)
    {
        double cx = ax[i] * bx[i] - ay[i] * by[i];
        double cy = ax[i] * by[i] + ay[i] * bx[i];
        ax[i] = cx, ay[i] = cy;
    }
    fft(ax, ay, len, 1);
    for (int i = 0; i < len; ++i)
        ans[i] = (int)(ax[i] + 0.5);
    return len;
}
string mul(string sa,string sb)
{
    int l1,l2,l;
    int i;
    string ans;
    memset(sum, 0, sizeof(sum));
    l1 = sa.size();
    l2 = sb.size();
    for(i = 0; i < l1; i++)
        x1[i] = sa[l1 - i - 1]-'0';
    for(i = 0; i < l2; i++)
        x2[i] = sb[l2-i-1]-'0';
    l = solve(x1, l1, x2, l2, sum);
    for(i = 0; i<l || sum[i] >= 10; i++)
    {
        sum[i + 1] += sum[i] / 10;
        sum[i] %= 10;
    }
    l = i;
    while(sum[l] <= 0 && l>0)
    for(i = l; i >= 0; i--)    ans+=sum[i] + '0';
    return ans;
}
string Pow(string a,int n)
{
    if(n==1) return a;
    if(n&1) return mul(Pow(a,n-1),a);
    string ans=Pow(a,n/2);
    return mul(ans,ans);
}
int main()
{
    cin.sync_with_stdio(false);
    string a;
    int b;
    while(cin>>a>>b) cout<<Pow(a,b)<<endl;
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

const int L=110;

string mul(string a, string b)  
{  
    string s;  
    int na[L], nb[L], nc[L], La = a.size(), Lb = b.size();
    fill(na,na+L,0); fill(nb,nb+L,0); fill(nc,nc+L,0); 
    for(int i = La - 1; i >= 0; i--) na[La - i] = a[i] - '0';
    for(int i = Lb - 1; i >= 0; i--) nb[Lb - i] = b[i] - '0';  
    for(int i = 1; i <= La; i++)  
        for(int j = 1; j <= Lb; j++)  
            nc[i + j - 1] += na[i] * nb[j];
    for(int i = 1; i <= La + Lb; i++)  
        nc[i + 1] += nc[i] / 10, nc[i] %= 10;
    if(nc[La + Lb]) s += nc[La + Lb] + '0'; 
    for(int i = La + Lb - 1; i >= 1; i--)  
        s += nc[i] + '0'; 
    return s;  
}  

int main()  
{  
    string a;
    int exponent;
    while(cin >> a >> exponent)
    {
        string result = "1";
        for (int i = 0; i < exponent; i++)
        {
            result = mul(result, a);
        }
        cout << result << endl;  
    }
    return 0;  
}
想知道小甲鱼最近在做啥?请访问 -> 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 | 显示全部楼层
sh-5.1$ cat main.cpp
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>

using std::cin, std::cout, std::endl;
using boost::multiprecision::cpp_int;
using boost::multiprecision::pow;

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

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 04:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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