• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Uva 11879 - Multiple of 17

你为何这么叼!

Multiple of 17

 Time limit: 1.000 seconds

 

Theorem: If you drop the last digit d of an integer n (n$ \ge$10), subtract 5d from the remaining integer, then the difference is a multiple of 17 if and only if n is a multiple of 17.

For example, 34 is a multiple of 17, because 3-20=-17 is a multiple of 17; 201 is not a multiple of 17, because 20-5=15 is not a multiple of 17.

Given a positive integer n, your task is to determine whether it is a multiple of 17.

Input 

There will be at most 10 test cases, each containing a single line with an integer n ( 1$ \le$n$ \le$10100). The input terminates with n = 0, which should not be processed.

Output 

For each case, print 1 if the corresponding integer is a multiple of 17, print 0 otherwise.

Sample Input 

34
201
2098765413
1717171717171717171717171717171717171717171717171718
0

Sample Output 

1
0
1
0

 


Problemsetter: Rujia Liu, Special Thanks: Yiming Li

 

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2013/9/28 星期六 12:54:45
  4 File Name     :2013长春网络赛\1004.cpp
  5 ************************************************ */
  6 
  7 #pragma comment(linker, "/STACK:1024000000,1024000000")
  8 #include <stdio.h>
  9 #include <string.h>
 10 #include <iostream>
 11 #include <algorithm>
 12 #include <vector>
 13 #include <queue>
 14 #include <set>
 15 #include <map>
 16 #include <string>
 17 #include <math.h>
 18 #include <stdlib.h>
 19 #include <time.h>
 20 using namespace std;
 21 /*
 22  * 完全大数模板
 23  * 输出cin>>a
 24  * 输出a.print();
 25  * 注意这个输入不能自动去掉前导0的,可以先读入到char数组,去掉前导0,再用构造函数。
 26  */
 27 #define MAXN 9999
 28 #define MAXSIZE 1010
 29 #define DLEN 4
 30 
 31 class BigNum
 32 {
 33 private:
 34     int a[MAXN];  //可以控制大数的位数
 35     int len;
 36 public:
 37     BigNum(){len=1;memset(a,0,sizeof(a));}  //构造函数
 38     BigNum(const int);     //将一个int类型的变量转化成大数
 39     BigNum(const char*);   //将一个字符串类型的变量转化为大数
 40     BigNum(const BigNum &); //拷贝构造函数
 41     BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算
 42     friend istream& operator>>(istream&,BigNum&); //重载输入运算符
 43     friend ostream& operator<<(ostream&,BigNum&); //重载输出运算符
 44 
 45     BigNum operator+(const BigNum &)const;  //重载加法运算符,两个大数之间的相加运算
 46     BigNum operator-(const BigNum &)const;  //重载减法运算符,两个大数之间的相减运算
 47     BigNum operator*(const BigNum &)const;  //重载乘法运算符,两个大数之间的相乘运算
 48     BigNum operator/(const int &)const;     //重载除法运算符,大数对一个整数进行相除运算
 49 
 50     BigNum operator^(const int &)const;     //大数的n次方运算
 51     int operator%(const int &)const;        //大数对一个int类型的变量进行取模运算
 52     bool operator>(const BigNum &T)const;   //大数和另一个大数的大小比较
 53     bool operator>(const int &t)const;      //大数和一个int类型的变量的大小比较
 54 
 55     void print();        //输出大数
 56 };
 57 BigNum::BigNum(const int b)   //将一个int类型的变量转化为大数
 58 {
 59     int c,d=b;
 60     len=0;
 61     memset(a,0,sizeof(a));
 62     while(d>MAXN)
 63     {
 64         c=d-(d/(MAXN+1))*(MAXN+1);
 65         d=d/(MAXN+1);
 66         a[len++]=c;
 67     }
 68     a[len++]=d;
 69 }
 70 BigNum::BigNum(const char *s)  //将一个字符串类型的变量转化为大数
 71 {
 72     int t,k,index,L,i;
 73     memset(a,0,sizeof(a));
 74     L=strlen(s);
 75     len=L/DLEN;
 76     if(L%DLEN)len++;
 77     index=0;
 78     for(i=L-1;i>=0;i-=DLEN)
 79     {
 80         t=0;
 81         k=i-DLEN+1;
 82         if(k<0)k=0;
 83         for(int j=k;j<=i;j++)
 84             t=t*10+s[j]-'0';
 85         a[index++]=t;
 86     }
 87 }
 88 BigNum::BigNum(const BigNum &T):len(T.len)  //拷贝构造函数
 89 {
 90     int i;
 91     memset(a,0,sizeof(a));
 92     for(i=0;i<len;i++)
 93         a[i]=T.a[i];
 94 }
 95 BigNum & BigNum::operator=(const BigNum &n)  //重载赋值运算符,大数之间赋值运算
 96 {
 97     int i;
 98     len=n.len;
 99     memset(a,0,sizeof(a));
100     for(i=0;i<len;i++)
101         a[i]=n.a[i];
102     return *this;
103 }
104 istream& operator>>(istream &in,BigNum &b)
105 {
106     char ch[MAXSIZE*4];
107     int i=-1;
108     in>>ch;
109     int L=strlen(ch);
110     int count=0,sum=0;
111     for(i=L-1;i>=0;)
112     {
113         sum=0;
114         int t=1;
115         for(int j=0;j<4&&i>=0;j++,i--,t*=10)
116         {
117             sum+=(ch[i]-'0')*t;
118         }
119         b.a[count]=sum;
120         count++;
121     }
122     b.len=count++;
123     return in;
124 }
125 ostream& operator<<(ostream& out,BigNum& b)  //重载输出运算符
126 {
127     int i;
128     cout<<b.a[b.len-1];
129     for(i=b.len-2;i>=0;i--)
130     {
131         printf("%04d",b.a[i]);
132     }
133     return out;
134 }
135 BigNum BigNum::operator+(const BigNum &T)const   //两个大数之间的相加运算
136 {
137     BigNum t(*this);
138     int i,big;
139     big=T.len>len?T.len:len;
140     for(i=0;i<big;i++)
141     {
142         t.a[i]+=T.a[i];
143         if(t.a[i]>MAXN)
144         {
145             t.a[i+1]++;
146             t.a[i]-=MAXN+1;
147         }
148     }
149     if(t.a[big]!=0)
150        t.len=big+1;
151     else t.len=big;
152     return t;
153 }
154 BigNum BigNum::operator-(const BigNum &T)const  //两个大数之间的相减运算
155 {
156     int i,j,big;
157     bool flag;
158     BigNum t1,t2;
159     if(*this>T)
160     {
161         t1=*this;
162         t2=T;
163         flag=0;
164     }
165     else
166     {
167         t1=T;
168         t2=*this;
169         flag=1;
170     }
171     big=t1.len;
172     for(i=0;i<big;i++)
173     {
174         if(t1.a[i]<t2.a[i])
175         {
176             j=i+1;
177             while(t1.a[j]==0)
178                 j++;
179             t1.a[j--]--;
180             while(j>i)
181                 t1.a[j--]+=MAXN;
182             t1.a[i]+=MAXN+1-t2.a[i];
183         }
184         else t1.a[i]-=t2.a[i];
185     }
186     t1.len=big;
187     while(t1.a[len-1]==0 && t1.len>1)
188     {
189         t1.len--;
190         big--;
191     }
192     if(flag)
193         t1.a[big-1]=0-t1.a[big-1];
194     return t1;
195 }
196 BigNum BigNum::operator*(const BigNum &T)const  //两个大数之间的相乘
197 {
198     BigNum ret;
199     int i,j,up;
200     int temp,temp1;
201     for(i=0;i<len;i++)
202     {
203         up=0;
204         for(j=0;j<T.len;j++)
205         {
206             temp=a[i]*T.a[j]+ret.a[i+j]+up;
207             if(temp>MAXN)
208             {
209                 temp1=temp-temp/(MAXN+1)*(MAXN+1);
210                 up=temp/(MAXN+1);
211                 ret.a[i+j]=temp1;
212             }
213             else
214             {
215                 up=0;
216                 ret.a[i+j]=temp;
217             }
218         }
219         if(up!=0)
220            ret.a[i+j]=up;
221     }
222     ret.len=i+j;
223     while(ret.a[ret.len-1]==0 && ret.len>1)ret.len--;
224     return ret;
225 }
226 BigNum BigNum::operator/(const int &b)const  //大数对一个整数进行相除运算
227 {
228     BigNum ret;
229     int i,down=0;
230     for(i=len-1;i>=0;i--)
231     {
232         ret.a[i]=(a[i]+down*(MAXN+1))/b;
233         down=a[i]+down*(MAXN+1)-ret.a[i]*b;
234     }
235     ret.len=len;
236     while(ret.a[ret.len-1]==0 && ret.len>1)
237         ret.len--;
238     return ret;
239 }
240 int BigNum::operator%(const int &b)const   //大数对一个 int类型的变量进行取模
241 {
242     int i,d=0;
243     for(i=len-1;i>=0;i--)
244         d=((d*(MAXN+1))%b+a[i])%b;
245     return d;
246 }
247 BigNum BigNum::operator^(const int &n)const  //大数的n次方运算
248 {
249     BigNum t,ret(1);
250     int i;
251     if(n<0)exit(-1);
252     if(n==0)return 1;
253     if(n==1)return *this;
254     int m=n;
255     while(m>1)
256     {
257         t=*this;
258         for(i=1;(i<<1)<=m;i<<=1)
259            t=t*t;
260         m-=i;
261         ret=ret*t;
262         if(m==1)ret=ret*(*this);
263     }
264     return ret;
265 }
266 bool BigNum::operator>(const BigNum &T)const    //大数和另一个大数的大小比较
267 {
268     int ln;
269     if(len>T.len)return true;
270     else if(len==T.len)
271     {
272         ln=len-1;
273         while(a[ln]==T.a[ln]&&ln>=0)
274           ln--;
275         if(ln>=0 && a[ln]>T.a[ln])
276            return true;
277         else
278            return false;
279     }
280     else
281        return false;
282 }
283 bool BigNum::operator>(const int &t)const  //大数和一个int类型的变量的大小比较
284 {
285     BigNum b(t);
286     return *this>b;
287 }
288 void BigNum::print()   //输出大数
289 {
290     int i;
291     printf("%d",a[len-1]);
292     for(i=len-2;i>=0;i--)
293       printf("%04d",a[i]);
294     printf("\n");
295 }
296 
297 int main()
298 {
299     //freopen("in.txt","r",stdin);
300     //freopen("out.txt","w",stdout);
301     int m;
302     int T;
303     BigNum n;
304     while(cin>>n){
305         if(n>0);
306         else break;
307         BigNum t=(n%10)*5;
308         BigNum tt = n/10;
309         if(tt>t)tt=tt-t;
310         else tt=t-tt;
311         if(tt%17==0)printf("1\n");
312         else printf("0\n");
313     }
314     return 0;
315 }
View Code 2013-10-09 11:03:08 

 

posted @ 2013-10-09 11:02  HaibaraAi  阅读(195)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3