70分,求调

P1045 [NOIP2003 普及组] 麦森数

Huayecai @ 2024-09-25 19:49:45

#include<bits/stdc++.h>
using namespace std;

int n;

int m[5100]={1,1};

void cheng(int a[],int b,int c[])
{
    int u=0;
    int t=0;
    for(int i=1;i<=a[0];i++)
    {
        t=a[i]*b+u;
        c[i]=t%10;
        u=t/10;
    }
    int len=c[0];
    while(u>0)
    {
        len++;
        c[len]=u%10;
        u/=10;
    }

    c[0]=500;
}

int main(){
    int cnt=0;
    cin>>n;

    for(int i=1;i<=n;i++)
        cheng(m,2,m);

    cout<<int(log10(2)*n+1)<<"\n";

    m[1]--;
    for(int i=500;i>=1;i--)
    {
        cnt++;
        cout<<m[i];
        if(cnt%50==0)cout<<"\n";
    }

    return 0;
}

by jiang_yitao @ 2024-10-19 17:09:13

@Huayecai 看一下我的

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
#include <string.h>
void mul(int* a, int* b)
{
   int i, j, res[125];
   memset(res, 0, sizeof(res));
   for (i = 0; i < 125; i++)
       for (j = 0; j < 125 - i; j++)
       {
           res[i + j] += a[i] * b[j];
           res[i + j + 1] += res[i + j] / 10000;
           res[i + j] %= 10000;
       }
   memcpy(a, res, sizeof(res));
}
int p, result[125], base[125];
int main()
{
   int i;
   scanf("%d", &p);
   printf("%d\n", (int)(p * log10(2.0)) + 1);
   result[0] = 1;
   base[0] = 2;
   while (p)
   {
       if (p & 1)
           mul(result, base);
       p >>= 1;
       mul(base, base);
   }
   result[0]--;
   for (i = 124; i >= 0; i--)
       if (i % 25 == 12)
           printf("%02d\n%02d", result[i] / 100, result[i] % 100);
       else
       {
           printf("%04d", result[i]);
           if (i % 25 == 0)printf("\n");
       }
   return 0;
}

求关注


by Huayecai @ 2024-10-19 18:34:51

@jiang_yitao 感谢,已关


|