精华内容
下载资源
问答
  • Bull Math (大数乘法

    2019-03-05 13:33:59
    Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help hi...

    Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). 

    FJ asks that you do this yourself; don't use a special library function for the multiplication.

    Input

    * Lines 1..2: Each line contains a single decimal number.

    Output

    * Line 1: The exact product of the two input lines

    Sample Input

    11111111111111
    1111111111

    Sample Output

    12345679011110987654321

    思路:

    1.移位:对于两个数a=12,b=25,在相乘的时候我们让每一位数分别相乘,即a[0]*b[0]=2 , a[0]*b[1]=5 , a[1]*b[0]=4 , a[1]*b[1]=10,那么规律就是,对于两个数a[i] , b[j],所有i+j相同的数都要加到一起,所以我们要把5+4=9合并为一个数,也就是说,将每一位数相乘之后,我们实际得到了2,9,10三个没有进位的数

    2.进位:通过上面的操作,我们的2,9,10三个没有进位的数,下面就要进行进位,因为我们是把高位相乘得到的数放在前面,地位相乘得到的数放在后面,所以这三个数排列自然也是高位在前,地位在后,所以要从右向左进位,进位的方法就是a[i]+=a[i+1]/10 , a[i+1]%=10,如果放到这三个数上面,就是,9+10/10=10,然后10%10=0,这三个数变成2,10,0,再一次就是2+10/10=3,10%10=0,三个数变为3,0,0,而这正是我们要求的结果,在实际操作中,我们习惯于将数倒着存放,即将数存为10,9,2,这是为了进位方便,因为如果正序的话如果最高位发生进位我们就要将每一个数向后移动一位从而挪出一个空位,想想都麻烦,倒序的话因为是向后进位,想怎么进就怎么进

    代码:

      

    #include<iostream>
    using namespace std;
    int num[100];
    int main()
    {
    	string s1,s2;
    	cin>>s1>>s2;
    	int len1=s1.length();
    	int len2=s2.length();
    	int i,j;
    	//数位相乘 
    	for(i=0;i<len1;i++)
    	for(j=0;j<len2;j++)
    	{
    	   num[len1-1+len2-1-i-j]=num[len1-1+len2-1-i-j]+(s1[i]-'0')*(s2[j]-'0');	
    	} 
    	//进位
    	for(i=0;i<len1+len2;i++)
    	{
    		num[i+1]=num[i+1]+num[i]/10;
    		num[i]=num[i]%10;
    	} 
    	//寻找不为零的最高位
    	for(i=len1+len2-1;i>=0;i--)
    	{
    		if(num[i]!=0)
    		{
    			break;
    		}
    	}
    	//转换成字符串
    	string result=""; 
    	for(;i>=0;i--)
    	{
    		result=result+(char)(num[i]+'0');//一定要强制转换成字符 
    	} 
    	cout<<result<<endl;
       return 0;	
    } 


     

    展开全文
  • Bull Math(大数乘法

    2014-09-17 13:06:44
    Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are c

    Description

    Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros). 
    FJ asks that you do this yourself; don't use a special library function for the multiplication.

    Input

    * Lines 1..2: Each line contains a single decimal number.

    Output

    * Line 1: The exact product of the two input lines

    Sample Input

    11111111111111
    1111111111

    Sample Output

    12345679011110987654321
    #include<stdio.h>
    #include<string.h>
    #define Len 43 //数的最大长度
    int first[Len],second[Len],res[2*Len];
    char a[Len],b[Len];
    int lena,lenb;
    void Mul()
    {
    	memset(res,0,sizeof(res));
    	int i,j,carry=0;
    	for(i=1;i<=lena;i++)
    		for(j=1;j<=lenb;j++)
    		{
    			res[i+j-1]+=(first[i]*second[j]);
    			res[i+j]+=res[i+j-1]/10;
    			res[i+j-1]%=10;
    		}
    		res[i+j]+=res[i+j-1]/10;
    		res[i+j-1]%=10;
    		for(i=lena+lenb;i>0&&res[i]==0;i--);//去前导0
    		for(;i>=1;i--)
    			printf("%d",res[i]);
    		printf("\n");
    }
    
    int main()
    {
    	//freopen("b.txt","r",stdin);
    	while(scanf("%s %s",a,b)==2)
    	{
    		int i;
    		lena=strlen(a),lenb=strlen(b);
    		for(i=0;i<lena;i++)
    			first[i+1]=a[lena-1-i]-'0';
    		for(i=0;i<lenb;i++)
    			second[i+1]=b[lenb-1-i]-'0';
    		Mul();
    	}
    	return 0;
    }


    展开全文
  • A Simple Math Problem Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 8 Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Geo
    
    A Simple Math Problem
    Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 8   Accepted Submission(s) : 7

    Font: Times New Roman | Verdana | Georgia

    Font Size:

    Problem Description

    Lele now is thinking about a simple function f(x).

    If x < 10 f(x) = x.
    If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
    And ai(0<=i<=9) can only be 0 or 1 .

    Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

    Input

    The problem contains mutiple test cases.Please process to the end of file.
    In each case, there will be two lines.
    In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
    In the second line , there are ten integers represent a0 ~ a9.

    Output

    For each case, output f(k) % m in one line.

    Sample Input

    10 9999
    1 1 1 1 1 1 1 1 1 1
    20 500
    1 0 1 0 1 0 1 0 1 0
    

    Sample Output

    45
    104
    

    Author

    linle

    Source

    2007省赛集训队练习赛(6)_linle专场
    生气一开始,矩阵构造得不对,结果给出的数据过不了,真心恼火!!
    后来仔细想想后,发觉可以构造出更好的矩阵,与是就过了吐舌头
    {f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7],f[8],f[9]}*A={f[10],f[11],f[12],f[13],f[14],f[15],f[16],f[17],f[18],f[19]}
    只要构造出10*10矩阵A再用上矩阵快速幂就可以交上AC代码鸟~
    ~~AC不易,且行且珍惜
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <queue>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define inf 9999999
    const int MAXN = 10;
    int M[MAXN][MAXN];
    int f[MAXN];
    int a[MAXN];
    int m;
    struct Matrix{
        int M[MAXN][MAXN];
    }initm,unitm;
    void initM(){//构造矩阵,这一步很重要
        int i, j,k;
        for (i = 0; i < 10; i++){
            for (j = 0; j < 10; j++)
                initm.M[i][j] = 0;
        }
        for (i = 0; i < 10; i++){
            for (j = 0; j < 10-i;j++)
                initm.M[9-j][i] = a[j+i];
        }
        for (i = 1; i < 10; i++){
            for (k = 0; k < i;k++)
                for (j = 0; j < 10; j++)
                    initm.M[j][i] = (initm.M[j][i]+a[k]*initm.M[j][i - k-1]%m)%m;
        }
    	/*
    	for(i=0;i<10;i++){
    		for(j=0;j<10;j++){
    			printf("%d ",initm.M[i][j]);
    		}
    		printf("\n");
    	}*/
    }
    Matrix mul(Matrix a, Matrix b){ //让两个矩阵相乘
        Matrix c;
        int i, j,k;
        for (i = 0; i<10; i++){
            for (j = 0; j<10; j++){
                c.M[i][j] = 0;
                for (k = 0; k<10; k++){
                    c.M[i][j] = (c.M[i][j] + a.M[i][k] * b.M[k][j] % m) % m;
                }
                c.M[i][j] %= m;
            }
        }
        return c;
    }
    int MatrixFastM(Matrix p,Matrix q,int k,int r){  //矩阵快速幂
        int k1=k;
    	while (k>0){
            if (k & 1){
                p = mul(p, q);
            }
            q = mul(q, q);
            k >>= 1;
        }
        int nm = r - 10 * k1;
    	/*int i,j;
    	
    	printf("p.M%d\n",nm);
    	for(i=0;i<10;i++){
    		for(j=0;j<10;j++){
    			printf("%d ",p.M[i][j]);
    		}
    		printf("\n");
    	}*/
        return p.M[0][nm];
    }
    int main()
    {
        int k;
        int i, j;
        freopen("in.txt", "r", stdin);
        while (~scanf("%d%d", &k, &m)){
            memset(unitm.M, 0, sizeof(unitm.M));
            for (i = 0; i < 10; i++){
                unitm.M[0][i] = i;
            }
            for (i = 0; i < 10; i++){
                scanf("%d", &a[i]);
            }
            initM();
            int res = MatrixFastM(unitm, initm, k / 10,k);
            printf("%d\n", res%m);
        }
        return 0;
    }
    

    展开全文
  • 题目源地址:http://poj.org/problem?id=2389 程序

    题目源地址:http://poj.org/problem?id=2389

    程序源代码:

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    #include<memory.h>
    using namespace std;
    
    int main()
    {
        char s1[50],s2[50];//分别表示两个大数 ,char型 
        int ans[210];//结果数组,int型 
        int len1,len2;int i,j;
        
        memset(ans,0,sizeof(ans));
        cin>>s1>>s2;
        
        if(!strcmp(s1,"0") || !strcmp(s2,"0"))//如果s1=0或者s2=0,则直接输出结果0 
        {
           cout<<"0"<<endl;
        } 
        
        else
        {
            len1=strlen(s1);
            len2=strlen(s2);
            
            for(i=len2-1;i>=0;i--)
            {
               for(j=len1-1;j>=0;j--)
                  ans[200-(len1-1-j)-(len2-1-i)]+=(s1[j]-'0')*(s2[i]-'0');//注意:ans[200-(len1-1-j)-(len2-1-i)]
            }
            
            for(i=200;i>=0;i--)//进位操作 
            {
               if(ans[i]>9)
               {
                  ans[i-1]=ans[i-1]+ans[i]/10;
                  ans[i]=ans[i]%10;
               }
            }
            
            i=0;
            while(ans[i]==0)//利用i的值去掉前面无用的0 
               i++;
               
            //if(i>200)  若之前并没有判断s1和s2是否等于0,则在此处利用“0的个数大于200”也可判断相乘的结果为0,但是这样比较费时 
              // cout<<"0"<<endl;
              
            for( ;i<=200;i++)
               cout<<ans[i];
            cout<<endl;
        }
        //system("pause");
        return 0;
    }
    



    展开全文
  • 题意: 给出 两个10进制大数 求它们的积 ...import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); BigInteger
  • POJ-2389,Bull Math(大数乘法

    千次阅读 2020-01-24 13:21:35
    Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are c...
  • 简单的大数乘法。 #include <stdio.h> #include <string.h> #include <stdlib.h> void multiply_big(char *a, char *b, char *c){//大数乘法 int len1 = strlen(a), len2 = str...
  • A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441Accepted Submission(s): 1415 Problem Description Lele now ...
  • Bull MathTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatusDescriptionBulls are so much better at math than the cows. They can multiply huge integers together and
  • 因为 If x If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 所以根据递推式构造关系构造递推矩阵,与vijos1067的本质一样,...A Simple Math Problem Time Limit: 3000/100
  • hdu1757A Simple Math Problem 矩阵乘法

    千次阅读 2012-07-20 01:21:23
    hdu1757A Simple Math Problem   问题是怎么转化成矩阵相乘的呢?请参考:http://blog.csdn.net/fangzhiyang/article/details/6929747 看懂了思路再用自己的代码实现 (二维数组真心不方便,下次换结构体)  ...
  • 简单矩阵,注意相乘的时候是a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
  • Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help...
  • A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1697Accepted Submission(s): 959 Problem Description Lele now i...
  • 大整数乘法 题目分析 两个乘数都是大整数,相比于大整数乘int要适用的更广泛一些 乘法需要注意进位 相乘之后的结果要注意前导0,就是字符数组前面无意义的0,需要去掉 这道题说40位以内是假的。。。开数组的...
  • POJ 2389 Bull Math(大数乘法,还是Java好)

    千次阅读 2016-05-14 00:44:34
    Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14252   Accepted: 7350 Description Bulls are so much better at math than the cows. They can ...
  • #include using namespace std; int main() { char a[50],b[50],c[100]; int ans[100]; while(cin>>a>>b) { int lena=strlen(a); int lenb=s
  • Description ...Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers
  • 一,题意:  大整数乘法模板题二,思路:  1,模拟乘法(注意"逢十进一")  2,倒序输出(注意首位0不输出) 三,步骤:  如:555 x 35 = 19425 5 5 5 5 5 5 x 3 5  x 3 5  ----------- ==> ---------- ...
  • Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are cor...
  • 本文来自:... 1. 矩阵乘法: 矩阵乘积(matrix product,也叫matmul product): Hadamard product(又称element-wise product):用或者表示, 对应元素相乘,二者维数必须相同,举例: ...
  • 这是POJ上的一道大整数乘法水题,换了一种算法求解。  以前做大整数乘法的时候,是每乘完一趟,就用一次大整数加法。这次再做大整数乘法,换了一种算法,先统一乘完,再进位。代码简洁了不少。 #include #...
  • 高精乘法板子 然而WA了两次也是没救了 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=105; int la,lb,lc,a[N],b[N],c[N],tot; char ch[N]; int main...
  • 紫书上的高精度乘法改不来   t = int(input()) p =1 for i in range(t): s = int(input()) p =p* s print (p) 附:紫书上的高精度乘法: #include #define lson (rt) #define rson ...
  • Matrix mul(Matrix a, Matrix b) //矩阵乘法 { Matrix ret; ret.init(a.n, b.m); int i, j, k; for (i = 0; i ; i++) { for (j = 0; j ; j++) if (a.mat[i][j]) { for (k = 0; k ; k++) if (b....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 789
精华内容 315
关键字:

math乘法