• sicily 1231 The Embarrassed Cryptography


    其实就是给出一个数k,和一个上界L,找到小于这个上界L的最小的可以被k整除的数!

    问题在于大数k,同样只能用高精度来解决了,取模的时候从最高为开始模,然后把余数给下一位加上,再取模!

    而这里还用到了压缩的方法,每三个数字作为一个数储存,然后取模的时候也是一样,加余数的时候乘个1000就行了!

     1 //sicily 1231 The Embarrassed Cryptography
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <cstring>
     6 
     7 using namespace std;
     8 
     9 const int N = 1000000;
    10 bool a[N+1];
    11 char k[105];
    12 int hp[40];
    13 int thousand[]={1,10,100,1000};
    14 int len;
    15 
    16 //求素数(筛选法) 
    17 void get_prime()
    18 {
    19     memset(a,1,sizeof(a));
    20     for(int i=2; i<=N; i++)
    21     {
    22         if(a[i])
    23         {
    24             for(int j=2; j*i<=N; j++)
    25                 a[i*j]=0;
    26         }
    27     }
    28 } 
    29 //高精度,三位三位的分 
    30 void high_precision()  
    31 {  
    32     memset(hp, 0, sizeof(hp));  
    33     len=(strlen(k)-1)/3;  //注意偶数的时候 
    34     int klen=strlen(k);  
    35     for (int i=0; i<=len; i++)  
    36     {  
    37         for (int j=1; j<=3 && klen-i*3-j>=0; j++)   
    38             hp[i]+=thousand[j-1]*(k[klen-i*3-j]-'0');   
    39     }
    40     //for(int i=0; i<=len; i++)
    41     //    cout << hp[i] << " ";
    42     //cout << endl;
    43 }  
    44 //高精度取模 
    45 int is_mod(int x)
    46 {
    47     int c=0;
    48     for(int i=len; i>=0; i--)
    49         c=(hp[i]+c*1000)%x;
    50     return c;
    51 }
    52 
    53 
    54 int main()
    55 {
    56     int l;
    57     get_prime();
    58     while(scanf("%s%d", k, &l) && strcmp(k,"0") && l)
    59     {
    60         high_precision();
    61         int ans=0, flag=1;        
    62         
    63         for(int i=2; i<l; i++)
    64         {
    65             if(a[i] && is_mod(i) == 0)
    66             {
    67                 flag=0;
    68                 ans=i;
    69                 break;
    70             }
    71         }
    72         if(flag)
    73             printf("GOOD
    ");
    74         else
    75             printf("BAD %d
    ", ans);
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    2015.05.12:json的常用处理方式
    好菜都得花功夫做
    网站从 IIS6 迁移至 IIS7 后的通配符脚本映射权限配置不当可能导致403错误
    ELearning是学习系统而不是教育系统
    空难与软件开发(一)
    “模态”对话框和“后退”按钮
    闭包解析(Fun with closure)
    关于CultureInfo的两个问题
    从来就不可能精通:关于Boxing
    Windows RT 应用程序开发介绍培训的讲义
  • 原文地址:https://www.cnblogs.com/dominjune/p/4548149.html
Copyright © 2020-2023  润新知