问题 F: Binary to Prime
时间限制: 1 Sec 内存限制: 128 MB提交: 25 解决: 19
[ 提交][ 状态][ 讨论版]
题目描述
To facilitate the analysis of a DNA sequence, a DNA sequence is represented by a binary number. The group of DNA-1 has discovered a great new way . There is a certain correlation between binary number and prime number. Instead of using the ordinary decadic numbers, they use prime base numbers. Numbers in this base are expressed as sequences of zeros and ones similarly to the binary numbers, but the weights of bits in the representation are not powers of two, but the elements of the primes ( 2, 3, 5, 7,... ).
For example 01101 , ie. 2+5+7=14
Herefore, it is necessary to convert the binary number to the sum of prime numbers
输入
The input consists of several instances, each of them consisting of a single line. Each line of the input contains a 01 string, length of not more than 150. The end of input is not marked in any special way.
输出
For each test case generate a single line containing a single integer , The sum of the primes.
样例输入
000010001111001
样例输出
3520
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 200;
int weight[maxn]; ///put power in this array
int a[1010];
void Create_Table() ///make prime table
{
int num = 0;
memset(a,0,sizeof(a)); ///a[i] is equal to zero,a[i] is a prime
for(int i = 2; i <= 500; i++)
{
for(int j = 2*i; j <= 1000; j += i)
{
if(a[j]==0)
a[j] = 1;
}
}
for(int i = 2; i <= 1000; i++)
{
if(a[i]==0)
{
weight[num] = i;
num++;
}
}
}
int main()
{
Create_Table();
char str[200];
while(~scanf("%s",str))
{
int len = strlen(str);
int sum = 0;
for(int i = len-1; i >= 0; i--)
{
sum = sum + (str[i]-'0')*weight[len-i-1];
}
printf("%d\n",sum);
}
return 0;
}