ACM模拟题详解(2)——简单数论

ACM:数论专题(1)——素数的判定 (P.S: God!!!!!! 当前人类对于数学的研究所达到的高度已经远远超出了一个外行人所理解的程度, 俺是这么想的。。。。。。 ) 题目要求:     题目要求给定一个数字n,判定其是否为素数(n≤10^18)。素数:对于某个数字n,如果在所有不大于n的正整数中,只有1和n本身可以整除n,则n是一个素数。 解答:     素数的判定具有多种方法。     最朴素的方式是: 阅读详情

    有很多与数字相关的题目,主要考察基本的编程能力,如果数学比较好,对于解决这些问题有比较好的帮助。下面的题目是学生收集的题目,我进行了讲解。

1、Self Numbers

Description

In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ... The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.

Input

No input for this problem.

Output

Write a program to output all positive self-numbers less than 10000 in increasing order, one per line.

Sample Input

Sample Output

1
3
5
7
9
20
31
42
53
64
 |
 |       <-- a lot more numbers
 |
9903
9914
9925
9927
9938
9949
9960
9971
9982
9993

 

题目翻译:有这样的公式y=d(x1x2…xn)=x1+x2+…+xn+ x1x2…xn,例如:

d(123) = 123+1+2+3=129

d(55)=55+5+5=65

这里把x1x2…xn称为y的生成子,例如123是129的生成子,55是65的生成子。题目要求列出10000以内没有生成子的整数。

 

题目详解:对1到10000的生成子进行遍历,看能够生成哪些数字,然后把不能生成的输出即可,下面的代码供参考。

       public static void setNumbers(){
              int[] numbers=new int[10000];
              Arrays.fill(numbers, 0);
              for(int i=1;i<10000;i++){
                     int temp = i+i%10+(i/10)%10+(i/100)%10+i/1000;
                     if(temp<10000)
                            numbers[temp-1]=1;
              }
              for(int i=0;i<9999;i++){
                     if(numbers[i]==0)
                            System.out.println(i+1);
              }
       }

2、Goldbach's Conjecture

Description

In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

Every even number greater than 4 can be written as the sum of two odd prime numbers.

For example:

8 = 3 + 5. Both 3 and 5 are odd prime numbers.
20 = 3 + 17 = 7 + 13.
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.)

Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million.

Input

The input will contain one or more test cases.Each test case consists of one even integer n with 6 <= n < 1000000. Input will be terminated by a value of 0 for n.

Output

For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach's conjecture is wrong."

Sample Input

8
20
42
0

Sample Output

8 = 3 + 5
20 = 3 + 17
42 = 5 + 37

 

翻译:据推测大于4的偶数都可以写成两个质数(素数)的和。让我们编写程序进行验证,如果有写出表达式,如果没有则输出“Goldbach's conjecture is wrong.”。如果能够写成的式子多于1个,则写出两个数字差比较大的那对。例如:

42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23

输出42 = 5 + 37即可。因为37-5最大。

 

题目详解:题目比较简单,只要测试两个数是否为质数即可。下面的代码供参考。

    /*
     * 8 = 3 + 5
     * 20 = 3 + 17
     * 42 = 5 + 37
     */
    public static void test(int x){
       if(6<=x && x < 1000000){
           if(x%2!=0){
              System.out.println("输入数据不是偶数!");
           }else{
              boolean b=false;
              for(int i=3;i+i<x;i++){
                  if(isPrime(i) && isPrime(x-i)){
                     System.out.println(x+" = "+i+" + "+(x-i));
                     b = true;
                     break;
                  }
              }
              if(!b)
                  System.out.println("Goldbach's conjecture is wrong.");
           }
       }else{
           System.out.println("输入数据范围不对");
       }
    }
    
    /*
     * 判断某个数是否为质数,如果是返回true
     */
    public static boolean isPrime(int x){
       for(int i=2;i*i<=x;i++){
           if(x%i==0)
              return false;
       }
       return true;
    }

3、Sum of Consecutive Prime Numbers

Description

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. Your mission is to write a program that reports the number of representations for the given positive integer.

Input

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2
3
17
41
20
666
12
53
0
Sample Output
1
1
2
3
0
0
1
2

 

翻译:有些整数可以表示为多个连续质数的和,例如53 = 5 + 7 + 11 + 13 + 17,有的数字有多中表示方法,例如41可以表示为2+3+5+7+11+13, 11+13+17, and 41。题目要求计算给定的某个数有多少种表示方法。

 

题目详解:对于给定的某个数,先从2开始,然后找下一个质数,然后累加直到他们的和等于或者大于给定的数,如果等于说明是一组解,如果大于说明不是接,然后找下一个接,直到结束。下面的代码供参考。

    /*
     * Sum of Consecutive Prime Numbers
     */
    public static int test2(int x){
       int count=0;
       for(int i=2;i+i<x;i++){
           int sum = 0;
           if(!isPrime(i)){
              continue;
           }
           for(int j=i;j<x;j++){
              // 判断是否为质数
              if(!isPrime(j))
                  continue;
              sum = sum+j;
              // 判断是否满足条件
              if(sum>=x){
                  if(sum==x){
                     count++;
                  }
                  break;
              }
           }
       }
       if(isPrime(x))
           count++;
       return count;
    }
    
    /*
     * 判断某个数是否为质数,如果是返回true
     */
    public static boolean isPrime(int x){
       for(int i=2;i*i<=x;i++){
           if(x%i==0)
              return false;
       }
       return true;
    }

4、Binomial Coefficients

Description

The binomial coefficient C(n, k) has been extensively studied for its importance in combinatorics. Binomial coefficients can be recursively defined as follows:

C(n, 0) = C(n, n) = 1 for all n > 0;
C(n, k) = C(n
− 1, k − 1) + C(n − 1, k) for all 0 < k < n.

Given n and k, you are to determine the parity of C(n, k).

Input

The input contains multiple test cases. Each test case consists of a pair of integers n and k (0 ≤ k ≤ n < 231, n > 0) on a separate line.

End of file (EOF) indicates the end of input.

Output

For each test case, output one line containing either a “0” or a “1”, which is the remainder of C(n, k) divided by two.

Sample Input

1 1
1 0
2 1

Sample Output

1
1
0

 

翻译:二项式系数(可能翻译的不好)C(n, k)满足下面的要求:

C(n, 0) = C(n, n) = 1 for all n > 0;
C(n, k) = C(n
− 1, k − 1) + C(n − 1, k) for all 0 < k < n.

题目要求根据给定的n和k(0 ≤ k ≤ n < 231, n > 0)计算C(n,k),典型的递归问题。但是如果采用递归,当n的值比较大的时候,会堆栈益处。而上面的表达式应该有相应的公式。如果采用公式计算就会变的简单了。公式自己找吧。

ACM数论专题1——gcd与lcm ACM数论专题1——gcd与lcm前言最大公约数和最小公倍数是什么?如何求得最大公因数和最小公倍数?最小公倍数的求解与证明求解证明最大公因数的证明与求解证明求解相关应用以及扩展公式相关应用太多了先挖坑吧扩展公式 前言 作为一个数学选手没有数学部分的总结是不完美滴~~ 既然有句话叫“数论只会gcd”,那就从gcd开始吧~ 最大公约数和最小公倍数是什么? 最大公因数1 也称最大公约数,指两个或多个整数... 阅读详情

相关推荐

ACM数论专题2——快速幂

ACM数论专题2——快速幂前言为什么要学快速幂快速幂算法核心思想快速幂实现快速幂的时间复杂度 前言 其实我就是想说说废话…刚学C语言两个月的时候我自己把快速幂用非递归的形式写出来了… 当时还不知道什么是快速幂…年轻真好T^T 为什么要学快速幂 我们在求形如aba^{b}ab的值时有很多选择~ 可以枚举,也可以用math里面的pow函数,也可以log换底(啊这个太难了我后面再填坑T^T) 按照朴素算...

qq_37136305的博客 522

java在ACM中的应用

 ACM模拟题讲解(1)-高精度       Java中提供了byte、short、int和long表示整数,float和double来表示浮点数,每种类型都有一定的表示范围,当超过了这个范围之后就不能处理了。为了提供对非常大的整数和浮点数的处理,Java提供了BigDecimal和BigInteger。下面的代码演示了BigDecimal和BigInteger的基本用法:     

自由女神 2149

ACM:数论专题(2)——Eular质数筛法

题目要求:     题目要求给定一个数字n,求在区间[2, n]范围内素数的个数。 解答:     本题还是比较简单的。主要利用了素数最基本的一个性质,即:素数只能被1和其本身整除。因此,如果能为某个数字找到一个除1和它本身以外的因数,那么它一定不是素数。     因此,开设一个大小为n的标记数组is_prime, 开始时其值均为true,然后从2开始扫描,首先2,是一个素数,因此

<code> I am a coder </code> 1013

hdu-1128 自私的数

Self Numbers In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d

Rainbow__sea的博客 409

ACM算法中数论核心知识详解与实战解析

数论作为ACM算法竞赛中的核心模块之一,是解决整数性质、同余关系与模运算等问题的理论基石。本章系统梳理整除性、最大公约数(GCD)、最小公倍数(LCM)的数学定义及其性质,深入讲解带余除法原理与唯一分解定理——即任一大于1的整数均可唯一表示为有限个素数的乘积。通过形式化定义模算术中的加、减、乘运算规则,建立同余关系的基本框架,为后续章节中快速幂、扩展欧几里得及中国剩余定理的学习奠定基础。同时,引入数论在现代密码学(如RSA)、哈希函数设计和伪随机数生成器中的实际应用,增强理论学习的工程指向性与现实意义。

weixin_42599908的博客 907

ESP32之 ESP-IDF + Clion 开发环境搭建(二)—— Linux版

本文介绍了 Linux 下,使用Clion IDE 用官方 ESP-IDF 开发包进行 ESP32 开发的环境搭建

Augtons 的博客 6116

CSUFT 个人赛(2) C题(POJ - 1316) D题(HDU - 2076) G题(HihoCoder - 1740) H题(HDU - 5640)

简单的题目 也没有考虑周全......

OneLine_的博客 566

论文理解【LLM-Clarification】—— 【QDrawer】Asking the Right Question at the Right Time: Human and Model Unce

本文基于 CoDraw 任务对 LLM 澄清提问的触发方式进行探索,发现模型内部不确定性与人类澄清决策相关性弱,使用不确定性阈值触发澄清提问相比模仿人类提问时机,性能和计算效率都更好

佚失的诗篇 794

(ZJU-2005复试)-HDOJ-1180-Self Numbers

 Self Numbers Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1927    Accepted Submission(s): 828  Problem DescriptionIn 1949

轻疯 清风 1215

Simulink实战:5步搞定固定翼飞行器六自由度动力学建模(附模型下载)

本文提供了一份详细的Simulink实战指南,通过5个核心步骤手把手教你构建固定翼飞行器的六自由度动力学模型。内容涵盖从顶层架构设计、核心方程手动实现,到模型集成、测试调试及进阶优化的完整流程,旨在帮助航空航天学生和工程师将抽象理论转化为可运行的仿真模型,并附有模型下载。

perl8的博客 646

tanner2019安装教程

tanner2019安装教程 tanner安装包 软件由老师提供,但由于老师给的方法不知怎么回事无法使用就自己找了一些方法,本教程仅提供参考 链接:https://pan.baidu.com/s/1A2O6JuEF8YFbe8bPnD3J6w 提取码:ysif 复制这段内容后打开百度网盘手机App,操作更方便哦 安装过程 1.选择TannerTools_v2019.2-Win64.exe,进行安装。 2.选择安装产品 3.选择目标路径 4.确认安装 5.安装成功,退出。 获取许可过程 1.打开key压缩

weixin_47730560的博客 1万+

hoj 1087 Self Numbers

In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for dig

GadyPu的专栏 510

Dify保姆级教程:从零搭建AI知识库,让大模型真正懂你的文档

通用大模型的知识边界来自公开语料,面对企业内部制度、产品手册等私有内容,往往只能靠“猜”,由此产生幻觉。要解决这个问题,需要借助检索增强生成(RAG)技术,为模型配置一个外挂资料库。RAG的原理类似开卷考试:先对文档进行文本切分、向量化,再在提问时检索语义相关的片段,交由大模型生成答案。Dify作为开源AI知识库平台,将文档导入、分段设置、向量索引、召回测试等复杂链路封装成可视化操作,让产品经理、运营等非研发人员也能独立搭建企业级问答助手。本文从环境部署、模型接入、参数调优到多知识库维护,完整覆盖一条可落地

weixin_29039773的博客 233

【更新完毕】《算法竞赛中的初等数论》(ACM / OI / MO)前言、后记、目录索引(十五万字符的数论书)

《算法竞赛中的初等数论》(信奥 / 数竞 / ACM)由我个人整理的算法竞赛数论合集,近五万字,近十五万字符,算法竞赛,数论相关,这一篇就够了

繁凡さん的博客 3万+

Applied Energy期刊小白论文投稿模板-流程经验分享

Applied Energy期刊小白论文投稿模板-流程经验分享,Elsevier期刊(爱思唯尔)投稿流程

zzu_Flyer的博客 4万+

WOFOST作物模型(3):敏感性分析

使用TAGP(Total Above Ground Production),地上总产量TSUM1,temperature sum from emergence to anthesis,萌发至开花所需温度TSUM2,temperature sum from anthesis to maturity,开花至成熟所需温度SPAN,life span of leaves growing at 35 Celsius,生长温度为35度时的叶龄。

所有专家都曾始于新手 1149

ACM在线模版-f-zyj

试图打造最为完善最为工整的ACM竞赛模版!!!By-f-zyj 在线速查!赛前整理打印,为比赛做足准备-_-#

逐梦者 11万+

执行HDevelop项目

这一节,我们讲述的是如何用HDevEngine加载和执行一个HDevelop项目。代码部分来源于示例程序exec_program(源文件exec_program.cpp),检测塑料零件边界上的毛刺。1.Step 1:初始化首先,我们包含HALCON/C++和HDevEngine头文件以及命名空间。注意到在这个示例程序中,HDevEngine头文件已经包含在my_hdevoperatorimpl中,所

Z's Palace 3005

ACM模块解析之 数论

数  论一.简介数论是ACM中的重点内容。历年竞赛题目,一般都有1~2道题目与数论有密切关系。数论涉及的概念和算法很多,用途也非常广泛。掌握与数论有关的方法,是参赛者需要具备的必要技能。数论的学习应该注意简单的概念要能熟练掌握,可以手写代码。较难理解的概念和知识点应注意模板的整理,模板的整理要注意规范接地气,变量名函数名简单易懂通用,写明模板函数的作用,在比赛中能快速上手使用,最好是本人理解后所写...

popcjz的博客 784

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【数论】ACM数论基础知识总结

文章目录一.质数1.定义2.质数的判断3.质数的筛选4.质因子分解5.互质二.同余1.模运算 数论是数学的一个分支,研究的是整数的性质。本篇文章总结的是数论在ACM中的常见应用。 一.质数 1.定义 质数,又称素数,若一个正整数无法被除了1和它自身以外的其它数整除,则称其为质数,否则为合数。特殊地,1既不是合数也不是质数 2.质数的判断 给定一个数,判断是否为质数的方法: 试除法: 检查所有可能成为n的因子的数,若没有找到因子,则证明这个数是一个质数 一种朴素的做法就是从2遍历到N-1观察有无N的因子,

Qiuker_jl的博客 3276

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

ACM中的数论

ACM中的数论是计算机科学领域中的一个重要分支,它主要研究整数的性质、运算规律和它们之间的关系。在ACM竞赛中,数论问题经常出现,因此掌握一定的数论知识对于参加ACM竞赛的选手来说是非常重要的。同余方程:形如x≡a(mod m)的方程,其中x是整数,a和m是已知整数。求解这类方程的方法称为同余方程的解法。离散对数问题:给定一个整数n和一个整数g,求解满足ax^2+by=n的整数解(x,y)的数量。密码学:在密码学中,很多加密算法都涉及到大整数的乘法和取模运算,例如RSA加密算法、椭圆曲线加密算法等。

想要AC的sjh的博客空间 1175

ACM中数论方面题目集

ACM中数论方面题目集 整除问题: hdu2009 nefu115 nefu116 pku1061 nefu84  素数问题 nefu117 nefu2 hdu2098 nefu109 pku2689 nefu118 nefu119 nefu120  同余问题 pku2769 hdu1021 hdu2035 pku2891 pku2115 hdu1573 hdu3579 pku32

jyNext的专栏 3100
上一篇: ACM模拟题讲解(1)-高精度
下一篇: ACM模拟题详解(2)——简单数论
老李-大连
博客等级 码龄20年 3206粉丝 308原创
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址