Poj 1152 an easy problem! (Modulo operation)

Source: Internet
Author: User

Link: poj 1152 an easy problem!

Evaluate the number r of an n-base to ensure that R can be divisible by (N-1) with the smallest n.

The first reaction was violence. The size of N ranges from 0 to 62. It is found that data overflows when n is converted into 10. Here there is a division, that is, (N-1) modulo is 0.

Example: a1a2a3 represents an N-base number R and converts it to a 10-digit number:

(A1 * n + A2 * n + A3) % (N-1) = (A1 * n) % (N-1) + (A2 * n) % (N-1) + (A3) % (N-1) % (N-1) = (A1 + A2 + A3) % (N-1 );

This prevents data overflow.



AC code:


#include<stdio.h>#include<string.h>#define ll __int64#include<map>using namespace std;map<char,ll> mm;int main(){ll max,ans;ll n,i,len;char s[100]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";char ss[50000+10];mm.clear();for(i=0;i<strlen(s);i++){mm[s[i]]=i;//printf("(%c %d)\n",s[i],mm[s[i]]);}while(scanf("%s",ss)!=EOF){len=strlen(ss);ans=0;max=2;for(i=0;i<len;i++){if(max<mm[ss[i]])max=mm[ss[i]];ans+=mm[ss[i]];}for(i=max+1;i<63;i++){if(ans%(i-1)==0)break;}if(i>=63)printf("such number is impossible!\n");elseprintf("%d\n",i);}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.