Divide by Six
来源:互联网 发布:选择公理 知乎 编辑:程序博客网 时间:2024/06/03 06:27
无解时输出-1s
而不是WTF
数据可能有前导零
Input file: standard inputOutput file: standard output Time limit: 1 secondMemory limit: 512 mebibytes
A positive integer number n is written on a blackboard. It consists of not more than10510^5105 digits. You have to transform it into a mogicalnumber by erasing some of the digits, and you want to erase as few digits as possible.
The number is lucky if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 6. For example, 0, 66,66666 are lucky numbers, and 00, 25, 77 are not.
Write a program which for the given nnn will find a mogical number such that nnn can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the numbernnn.
Print the length of your answer after the erasing.
If it's impossible to obtain a lucky number, print -1s
.
Input
The first line of input contains nnn -- a positive integer ( 1≤n≤10100000 1\le n \le 10^{100000}1≤n≤10100000 ).
Output
Print one number — the number of your lucky number obtained by erasing as few as possible digits. If there is no answer, print-1s
.
Example
Input 1
0010456
Output 1
4
Input 2
11
Output 2
-1s
题意:让你消去最少的数,使得剩下的数能被6整除
思路:没用数位dp想,直接类似模拟一样的做法。。。但是好麻烦啊、、、、、首先先把前导0消去,因为6能分成2*3所以先从后往前找能被2整除的数,部能整除的就删去,其次看剩下的数的和除以3的余数,(如果余数是1那么你就从串种找除以3等于1的数,把它删了),,,(如果和除以3的余数等于2那么就从串中找除以3等于2的,然后删除,或者找出两个除以3等于1的把这两个删除,比较是删除余数等于2的删的更少还是删除两个余数为1的更少),如果你处理后剩下的数全是0,那么就输出1.比如10输出1。。。。。0000输出1。。。。020输出1.。。。。010输出1.接下来就是安心码代码了
附上后台数据:
19260817
输出5
6450362765
输出9
9320245275504928053098337072361897260959506520326292069484135483... (+36 bytes)
输出;98
0000000000000000000000000000100000000000000000000000000000
输出1
3298367532475768295308261521283917198563270909966042317999631013... (+99936 bytes)
输出:99998
#include <stdio.h>#include<iostream>#include<string.h>#include <algorithm>using namespace std;char a[100100];int c[100100];int c1[100100];int main(){ while(scanf("%s",a)!=-1) { int l=strlen(a); int p=0; int ni=l; int we; for(int i=0;i<l;i++) { if(a[i]=='0') p++; else { we=i; break; } } if(p==l) { printf("1\n"); } else { for(int i=l-1; i>=0; i--) { if((a[i]-'0')%2==1) { p++; ni=i; } else { break; } } if(ni==0) { printf("-1s\n"); } else { int sum=0; for(int i=0; i<ni; i++) { sum+=a[i]-'0'; } if(sum%3==1) { memset(c,0,sizeof(c)); int p1=0; int d=0; int o=0; for(int i=ni-1; i>=we; i--) { if((a[i]-'0')%3==1&&d==0) { d=1; } else { c[p1++]=a[i]-'0'; } } for(int i=p1-1;i>=0;i--) { if(c[i]==0) o++; else break; } if(d==0) { printf("-1s\n"); } else if(o==p1) { printf("1\n"); } else printf("%d\n",l-p-d-o); } if(sum%3==2) { memset(c,0,sizeof(c)); memset(c1,0,sizeof(c1)); int t=0; int t1=0; int minn; int ha=0; int p1=0; for(int i=ni-1; i>=we; i--) { if((a[i]-'0')%3==2&&ha==0) { ha=1; } else { c[p1++]=a[i]-'0'; } } for(int i=p1-1;i>=0;i--) { if(c[i]==0) t++; else break; } int p2=0; int k=0; for(int i=ni-1; i>=we; i--) { if((a[i]-'0')%2==1&&k<2) { k++; } else { c1[p2++]=a[i]-'0'; } } for(int i=p2-1;i>=0;i--) { if(c1[i]==0) { t1++; } else break; } if(ha==1&&k==2) { minn=min(k+t1,ha+t); printf("%d\n",l-p-minn); } else if(ha==0&&k==2) { minn=k+t1; if(t1==p2) printf("1\n"); else printf("%d\n",l-minn-p); } else if(ha==1&&k==0) { minn=ha+t; if(t==p1) printf("1\n"); else printf("%d\n",l-p-minn); } else if((ha==0&&k==0)) { printf("1s\n"); } } else if(sum%3==0) { printf("%d\n",l-p); } } } }}
#include<bits/stdc++.h>using namespace std;#define maxn 100010#define inf 0x3f3f3f3fint dp[maxn][7];char s[maxn];int main(){ scanf("%s",s+1); int len=strlen(s+1); memset(dp,inf,sizeof(dp)); bool flag=false; for(int i=1; i<=len; i++) { if(s[i]=='0') flag=true; else dp[i][(s[i]-'0')%6]=i-1; for(int j=0; j<6; j++) { dp[i][j]=min(dp[i-1][j]+1,dp[i][j]); for(int k=0; k<6; k++) { if((k*10+s[i]-'0')%6==j) { dp[i][j]=min(dp[i][j],dp[i-1][k]); } }// printf("%d %d : %d\n",i,j,dp[i][j]); } } if(dp[len][0]==inf&&!flag) printf("-1s"); else if(dp[len][0]!=inf) printf("%d\n",len-dp[len][0]); else printf("1\n"); return 0;}
- woj-Divide by Six
- Divide by Six
- WOJ 24. Divide by Six
- woj Divide by Six 数位dp
- woj~24. Divide by Six(dp)
- 17年武汉大学网络赛—Divide by Six
- woj-Divide by Six (dp)(方法巧妙)
- dp处理大数整除6的最大位数Divide by Six
- 2017武大校赛网络预选赛C题 Divide by Six
- IDL divide by zero
- Divide by Three codeforces
- Divide by Three
- Divide by Three
- Last Digit By Factorial Divide
- six
- Six java books in one archive by qawa
- Step-by-step learning C + +(chapter six summary____Statements)
- fzu 1912 Divisibility by Thirty-six(数论+枚举)
- 回溯法(一)——n皇后问题
- Hadoop集群伪分布式搭建
- linux内存管理浅析
- mysql慢查询优化步骤slow_query
- Android Zxing实现生成二维码功能
- Divide by Six
- Linux 下 ~ 与 / 的含义
- 随机生成几位数字字母组合
- 集合相等
- 如何防止SQL注入
- POJ2398 Toy Storage 判断点和线的关系+二分+计数
- (8)适配器模式
- 同步异步,阻塞非阻塞
- 使用 QJsonDocument 处理 JSON