pku1365 Prime Land (数论,合数分解模板)

题意:给你一个个数对a, b 表示ab这样的每个数相乘的一个数n,求n-1的质数因子并且每个指数因子k所对应的次数 h.

先把合数分解模板乖乖放上:

for (int i = 2; ans != 1; ++i)
        {
            if (ans%i == 0)
            {
                num[cnt] = i;    int k = 0;
                while (ans%i == 0){ ++k; ans /= i; }
                index[cnt++] = k;
            }
            if (i > 10000)break;
        }
        if (ans != 1){ num[cnt] = ans; index[cnt++] = 1; }

然后,我自己写了个快速幂

快速幂的模板:

ll pow(ll a, ll n)
{
    ll res;
    for (res = 1; n;a=a*a, n>>=1)
    if (n & 1) res = res*a;
    return res;
}

AC代码:

#include<cstdio>
#include<cstring>
#define ll long long
int num[1000];
int index[1000];
ll pow(ll a, ll n)
{
    ll res;
    for (res = 1; n;a=a*a, n>>=1)
    if (n & 1) res = res*a;
    return res;
}
int main()
{
    while (1){
        ll a, b, ans = 1;
        while (scanf("%lld", &a), a!=0){
            scanf("%lld", &b);
            ans *= pow(a, b);
            char nn=getchar();
            if (nn == ‘\n‘)break;
        }
        if (a == 0)break;
        ans--;
        int cnt = 0;
        for (int i = 2; ans != 1; ++i)
        {
            if (ans%i == 0)
            {
                num[cnt] = i;    int k = 0;
                while (ans%i == 0){ ++k; ans /= i; }
                index[cnt++] = k;
            }
            if (i > 10000)break;
        }
        if (ans != 1){ num[cnt] = ans; index[cnt++] = 1; }
        for (int i = cnt-1; i >= 0; --i)
            printf("%d %d%c", num[i], index[i], " \n"[i == 0]);
    }
}

原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/9653049.html

时间: 2024-10-26 15:02:02

pku1365 Prime Land (数论,合数分解模板)的相关文章

POJ1365 Prime Land【质因数分解】【素数】【水题】

题目链接: http://poj.org/problem?id=1365 题目大意: 告诉你一个数的质因数x的全部底数pi和幂ei.输出x-1的质因数的全部底数和幂 解题思路: 这道题不难.可是题意特别不好理解.对于我这样的英文渣的人.愣是一个小时没看明确 关于题意举例说明吧 比如 509 1 59 1 x = 509^1 * 59^1 = 30031 x-1 = 30030 则答案 13 1 11 1 7 1 5 1 3 1 2 1 就是 x-1 = 13^1 * 11^1 * 7^1 * 5

[暑假集训--数论]poj1365 Prime Land

Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,... denote the increasing sequence of all prime numbers. We know that x > 1 can be represented in only

HDU3988-Harry Potter and the Hide Story(数论-质因数分解)

Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2193    Accepted Submission(s): 530 Problem Description iSea is tired of writing the story of Harry Potter, so,

hdu_4497GCD and LCM(合数分解)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2151    Accepted Submission(s): 955 Problem Description Given two positive integer

hdu 5317 合数分解+预处理

RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2818    Accepted Submission(s): 1108 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and m

HDU 5317 RGCDQ (合数分解+预处理)

题目链接:HDU 5317 RGCDQ 题意:定义函数F(x)为x的不同的素因子且小于等于x的个数,询问[l,r]区间中gcd(F(i),F(j))的最大值. 思路:暴力预处理出所有的合数分解结果,发现F(x)最大也只有7,之后就是暴力求出所有1到7出现次数的前缀和.询问的时候就打到O(1)了. AC代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #

HDU4497 GCD and LCM 数论 素数分解

题意很简单首先以前做最简单的LCM跟CGD的时候都知道先求出两个数A,B的最大公约数GCD,那么LCM可以利用  A*B/GCD来求得,这点一开始脑残了没想到,结果没有进行特盘所以错了,意思就是 题目给的L%G不为0的话就是无解,结果我给判其它的去了,肯定漏了些什么没有发现 然后对于 L/G进行素因子分解,同时任意的数都能够通过素因子分解来表示,所以三个解x,y,z也能分解 L/G = p1^q1*p2^q2.... x = p1^i1*... y = p1^j1*... z = p1^k1*.

POJ 1365 Prime Land

Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2972 Accepted: 1362 Description Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,

UVA 10140 - Prime Distance(数论)

10140 - Prime Distance 题目链接 题意:求[l,r]区间内最近和最远的素数对. 思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案.注意1的情况,一开始没判断1,结果WA了 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define INF 0x3f