hdu1313 Round and Round We Go (大数乘法)
发布时间:2020-12-14 02:03:12 所属栏目:大数据 来源:网络整理
导读:Problem Description A cyclic number is an integer n digits in length which,when multiplied by any integer from 1 to n,yields a ~{!0~}cycle~{!1~} of the digits of the original number. That is,if you consider the number after the last digit
Problem Description
A cyclic number is an integer n digits in length which,when multiplied by any integer from 1 to n,yields a ~{!0~}cycle~{!1~} of the digits of the original number. That is,if you consider the number after the last digit to ~{!0~}wrap around~{!1~} back to the first digit,the sequence of digits in both numbers will be the same,though they may start at different positions.
For example,the number 142857 is cyclic,as illustrated by the following table:? 142857*1=142857 142857*2=285714 142857*3=428571 142857*4=571428 142857*5=714285 142857*6=857142 Write a program which will determine whether or not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed,they are considered part of the number and count in determining n. Thus,~{!0~}01~{!1~} is a two-digit number,distinct from ~{!0~}1~{!1~} which is a one-digit number.)
?
Output
For each input integer,write a line in the output indicating whether or not it is cyclic.
?
Sample Input
?
Sample Output
?
题意:给你一个字符串,让你判断它是不是一个“循环串”,循环串的定义是这个字符串所对应的整数乘上1~n(它的长度)的任何一个数,所得的结果为这个字符串循环后所得的整数。
思路:因为长度最多只有60,所以直接模拟就行了,附上大数乘法模板。
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; typedef long long ll; typedef long double ldb; #define inf 99999999 #define pi acos(-1.0) #define eps 1e-15 #define maxn 200 #define Len 3000//大数的长度 using namespace std; int len; char a[Len],b[Len],c[Len]; void Mul(char a[],char b[],char c[])//大数乘法 { int i,j,alen,blen,clen; for(i=0; i<Len;i++){ c[i]='0'; } c[Len]=' '; alen=strlen(a); blen=strlen(b); reverse(a,a+alen); reverse(b,b+blen); int sum=0; for(i=0; i<alen; i++){ for(j=0; j<blen; j++){ sum+=c[i+j]-'0'+(a[i]-'0')*(b[j]-'0'); c[i+j]=(sum%10)+'0'; sum/=10; } while(sum){ c[i+j++]+=sum%10; sum/=10; } } clen=len; c[clen]=' '; reverse(c,c+clen); } char str[700],str1[700],str2[700]; struct node{ char s[70]; }d[70]; bool cmp(node a,node b){ return strcmp(a.s,b.s)<0; } int main() { int n,m,i,tot,blen; while(scanf("%s",str1)!=EOF) { len=strlen(str1); for(i=0;i<len;i++){ str[i]=str1[i]; str[i+len]=str1[i]; } str[2*len]=' '; for(i=1;i<=len;i++){ tot=i-1; for(j=0;j<len;j++){ d[i].s[j]=str[tot];tot++; } d[i].s[len]=' '; } sort(d+1,d+1+len,cmp); int flag=1; for(i=2;i<=len;i++){ alen=len; for(j=0;j<len;j++){ a[j]=str[j]; } a[alen]=' '; int tt=i; blen=0; while(tt){ b[blen++]=tt%10+'0'; tt/=10; } b[blen]=' '; reverse(b,b+blen); Mul(a,b,c); if(strcmp(c,d[i].s)!=0){ flag=0; } } if(flag)printf("%s is cyclicn",str1); else printf("%s is not cyclicn",str1); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |