The Embarrassed Cryptographer POJ - 2635 同余模+高精度处理 +线性欧拉筛(每n位一起处理)

题意:给出两数乘积K(1e100) 和 一个数L(1e6)  问有没有小于L(不能等于)的素数是K的因数

思路:把数K切割 用1000进制表示   由同余模公式知   k%x=(a*1000%x+b*1000*1000%x+c*1000*1000*1000%x....)

a b c等为 相应位置的三位数  这样切割可以减少模的次数 防止超时

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 #include<cmath>
 5 #include<iostream>
 6 using namespace std;
 7 const int maxn=1e6+1000;
 8 int primes[maxn];
 9 int vis[maxn];
10 int cnt;
11 void init(){
12      cnt=0;
13     for(int i=2;i<maxn;i++){
14         if(!vis[i])primes[cnt++]=i;
15         for(int j=0;j<cnt&&i*primes[j]<maxn;j++){
16             vis[i*primes[j]]=1;
17             if(i%primes[j]==0)break;
18         }
19     }
20 }
21 char k[1000];
22 int kt[1000];
23 int l;
24 int lenkt;
25 bool check(int prime){
26     int ans=0;
27     for(int i=lenkt-1;i>=0;i--){
28         ans=(ans*1000+kt[i])%prime;
29     }
30     if(ans==0)return 1;
31     return 0;
32 }
33 int main(){
34     init();
35     while(scanf("%s%d",k,&l)==2&&l&&k[0]!=‘0‘){
36         int len=strlen(k);
37         memset(kt,0,sizeof(kt));
38         for(int i=0;i<len;i++){
39             int temp=(len-i+2)/3-1;//从后往前3个3个数 +2向上取整
40             kt[temp]=kt[temp]*10+k[i]-‘0‘;
41         }
42          lenkt=(len+2)/3;//+2是向上取整
43          int flag=0;
44          for(int i=0;i<cnt&&primes[i]<l;i++){
45              if(check(primes[i])){
46                  printf("BAD %d\n",primes[i]);
47                  flag=1;
48                  break;
49              }
50
51          }
52          if(!flag){
53              printf("GOOD\n");
54          }
55     }
56     return 0;
57 }

原文地址:https://www.cnblogs.com/ttttttttrx/p/10279677.html

时间: 2024-10-08 21:05:39

The Embarrassed Cryptographer POJ - 2635 同余模+高精度处理 +线性欧拉筛(每n位一起处理)的相关文章

Dirichlet&#39;s Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

题意 给出a d n    给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 bool Is_Primes[1000005]; 5 int Primes[1000005]; 6 int A[1000005]; 7 int cnt; 8 void Prime(int n){ 9 cnt=0; 1

Goldbach&#39;s Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想

题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i]  i这个数是不是素数  在线性筛后面加个装桶循环即可 #include<cstdio> #include<cstring> using namespace std; bool Is_Primes[1000005]; int Primes[1000005]; int cnt; void Prime(int n){ cnt=0; memset(Is_

Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)

题意:给一个数 可以写出多少种  连续素数的合 思路:直接线性筛 筛素数 暴力找就行   (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net/nk_test/article/details/46242401 https://blog.csdn.net/qq_40873884/article/details/79124552 https://blog.csdn.net/baoli1008/article/details/50788512

poj 2154 Color(polya计数 + 欧拉函数优化)

http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目,旋转后一样的属于同一种,结果模p. n个珠子应该有n种旋转置换,每种置换的循环个数为gcd(i,n).如果直接枚举i,显然不行.但是我们可以缩小枚举的数目.改为枚举每个循环节的长度L,那么相应的循环节数是n/L.所以我们只需求出每个L有多少个i满足gcd(i,n)= n/L,就得到了循环节数为n/L的个数.重点就是求出这样的i的个数. 令cnt = gcd(i,n) =

POJ 2480 Longge&#39;s problem (欧拉函数+乘性函数)

Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7343   Accepted: 2422 Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now

题解报告:poj 2480 Longge&#39;s problem(欧拉函数)

Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N. 

POJ 2407-Relatives(求一个整数的欧拉函数值)

Relatives Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2407 Appoint description:  System Crawler  (2015-04-04) Description Given n, a positive integer, how many positive integers less than n

POJ 1392 Ouroboros Snake(数位欧拉)

题目链接:http://poj.org/problem?id=1392 题目大意:题意看的我头痛,其实跟HDU2894差不多,但是这题要求输出这条路径上第k个数,而不是输出路径. 解题思路:也跟HDU2894差不多.... 代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define CLR(arr,val) memset(arr,val,sizeof(arr)) 5 using na

POJ 3126 Prime Path (bfs+欧拉线性素数筛)

Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. - It is a matter of security to change such things every now