HUST 1599 Multiple(数学难题)

Multiple

Time Limit: 2 Sec   Memory Limit: 64 MB
Submissions: 197   Solved: 35

Description

Rocket323 loves math very much. One day, Rocket323 got a number string. He could choose some consecutive digits from the string to form a number. Rocket323loves 64 very much, so he wanted to know how many ways can he choose from the string so the number he got multiples of 64 ?
 
A number cannot have leading zeros. For example, 0, 64, 6464, 128 are numbers which multiple of 64 , but 12, 064, 00, 1234 are not.

Input

Multiple cases, in each test cases there is only one line consist a number string.
Length of the string is less than 3 * 10^5 .
 
Huge Input , scanf is recommended.

Output

Print the number of ways Rocket323 can choose some consecutive digits to form a number which multiples of 64.

Sample Input

64064

Sample Output

5

HINT

There are five substrings which multiples of 64.
[64]064
640[64]
64[0]64
[64064]
[640]64

题目大意是:给出一个串很长的数,计算这里边的一部分是64倍数的总数,这一部分数字可以是单独的0,除此以外不能含有前导0

思路:2的6次方为64,如果一个数的后6位是64的倍数,那么它的前面加任何数都是64的倍数,这种方法就能求出数字长度大于6的所有数中满足条件的个数,再求出数字长度小于等于6的数中满足条件的个数,相加就好了,注意前导0

View Code
 1 # include<stdio.h>
 2 # include<string.h>
 3 char s[300005];
 4 int a[300005];    //用来记录到第i个字符出现0的个数
 5 int len,ans; //ans为结果
 6 int main(){
 7     int i,j,k;
 8     while(scanf("%s",s)!=EOF){
 9         len =strlen(s);
10         ans=0;
11         if(s[0]=='0') { ans =1; a[0]=1;}    //0满足条件
12         else a[0]=0;
13         
14         for(i=1;i<len;i++)
15             if(s[i]=='0'){ 
16                 a[i] = a[i-1]+1;
17                 ans++;
18             }
19             else
20                 a[i] = a[i-1];
21             //2到6个数字组成的数是64倍数的个数
22             for(i=2;i<=6;i++)
23             {
24                 for(j=0;j<=len-i;j++)
25                 {
26                     if(s[j]=='0')
27                         continue;
28                     int temp=0;
29                     for(k=j;k<j+i;k++)
30                     {
31                         temp *= 10;
32                         temp += (s[k]-'0');
33                     }
34                     if(temp%64 == 0)
35                         ans++;
36                 }
37             }
38             
39             //大于6位数字组成的数满足条件的个数
40             for(i=len-6;i>=0;i--)
41             {
42                 int temp = (s[i]-'0')*100000 + (s[i+1]-'0')*10000 + (s[i+2]-'0')*1000
43                     + (s[i+3]-'0')*100 + (s[i+4]-'0')*10 + (s[i+5]-'0');
44                 if(temp % 64 ==0)
45                     ans = ans+ i - a[i-1];
46             }
47             printf("%d\n",ans);
48     }
49     return 0;
50 }

 

你可能感兴趣的:(IP)