2 solutions

  • 1
    @ 2022-1-5 21:29:54

    用a从高到低的每一位加上前一位余数*10来对b进行%,最后得到的结果就是a%b的结果

    #include<stdio.h>
    const int N = 1e3+7;
    int b;
    char a[N];
    int divMod()
    {
    	int s = 0;
    	for(int i=0;a[i]!='\0';i++)
    		s = (s*10+a[i]-'0')%b;
    	return s;
    }
    int main()
    {
        while(~scanf("%s %d",a,&b))
        {
        	int num = divMod();
        	printf("%d\n",num);
    	}
    	return 0;
     } 
    
    • 0
      @ 2022-1-18 17:18:41

      我写的代码比较麻烦,斗胆贴出来,思路就是把大数切成几段,按同余的公式通过加乘等手段将结果余出来

      #include<iostream>
      #include<cstring>
      using namespace std;
      
      const int N=1007;
      
      char a[N];
      long long int ans,b;
      
      int main(){
      	while(cin>>a){
      		ans=0;
      		cin>>b;
      		int n=strlen(a);
      		for(int i=0;i<n;i++){
      			long long int k=(a[n-i-1]-'0');
      			long long int l=1;
      			long long int j=i;
      			while(j>0){
      				if(k<100000)k*=10;
      				else l*=10;
      				if(l==100000){
      					k*=(l%b);
      					k%=b;
      					l=1;
      				}
      				j--;
      			}
      			k=(k%b)*(l%b); 
      			ans+=k;
      			ans%=b;
      		}
      		printf("%lld\n",ans);
      	}
      	return 0;
      }
      
      • 1

      Information

      ID
      284
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      4
      Tags
      # Submissions
      83
      Accepted
      38
      Uploaded By