CodeForces 1047C Enlarge GCD(数论)题解

题意:n个数的gcd是k,要你删掉最少的数使得删完后的数组的gcd > k

思路:先求出k,然后每个数除以k。然后找出出现次数最多的质因数即可。

代码:

#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5 + 10;
const int M = 1.5 * 1e7 + 10;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e4 + 7;
int gcd(int a, int b){
    return b == 0? a : gcd(b, a % b);
}
int a[maxn];
int p[4000], prime[4000], pn;
int num[M];
void init(){
    pn = 0;
    memset(p, 0, sizeof(p));
    for(int i = 2;  i < 4000; i++){
        if(!p[i]){
            prime[pn++] = i;
        }
        for(ll j = 0; j < pn && i * prime[j] < 4000; j++){
            p[i * prime[j]] = 1;
            if(i % prime[j] == 0) continue;
        }
    }
//    printf("%d\n", pn);
}
int main(){
    int n, need;
    init();
    scanf("%d", &n);
    memset(num, 0, sizeof(num));
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        if(i == 1) need = a[i];
        else need = gcd(need, a[i]);
    }
    for(int i = 1 ; i <= n; i++) a[i] /= need;
    int ans = -1;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < pn && prime[j] * prime[j] <= a[i]; j++){
            if(a[i] % prime[j] == 0){
                num[prime[j]]++;
                ans = max(ans, num[prime[j]]);
                while(a[i] % prime[j] == 0) a[i] /= prime[j];
            }
        }
        if(a[i] > 1){
            num[a[i]]++;
            ans = max(ans, num[a[i]]);
        }
    }
    if(ans == -1) printf("%d\n", ans);
    else printf("%d\n", n - ans);
    return 0;
}

原文地址:https://www.cnblogs.com/KirinSB/p/10919638.html

时间: 2024-11-15 00:40:16

CodeForces 1047C Enlarge GCD(数论)题解的相关文章

Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变一个数的值(注意不是真的改变),使得这个区间的gcd是小明所猜的数也算小明猜对.另一种操作就是真的修改某一点的值. 解题思路 这里我们使用线段树,维护区间内的gcd,判断的时候需要判断这个区间的左右子区间的gcd是不是小明猜的数的倍数或者就是小明猜的数,如果是,那么小明猜对了.否则就需要进入这个区间

HDU 3049 Data Processing 数论题解

Problem Description Chinachen is a football fanatic, and his favorite football club is Juventus fc. In order to buy a ticket of Juv, he finds a part-time job in Professor Qu's lab. And now, Chinachen have received an arduous task--Data Processing. Th

UVA 10951 - Polynomial GCD(数论)

UVA 10951 - Polynomial GCD 题目链接 题意:给定两个多项式,求多项式的gcd,要求首项次数为1,多项式中的运算都%n,并且n为素数. 思路:和gcd基本一样,只不过传入的是两个多项式,由于有%n这个条件,所以计算过程可以用乘法逆去计算除法模,然后最后输出的时候每项除掉首项的次数就是答案了. 代码: #include <stdio.h> #include <string.h> #include <vector> using namespace s

hdu 4910 Problem about GCD(数论)

题目连接:hdu 4910 Problem about GCD 题目大意:给定M,判断所有小于M并且和M互质的数的积取模M的值. 解题思路:有个数论的结论,若为偶数,M=M/2. 可以写成M=pk,即只有一种质因子时,答案为M-1,否则为1.特殊情况为4的倍数,不包括4. 首先用1e6以内的素数去试除,如果都不可以为p,那么对大于1e6的情况判断一下是否为素数,是素数也可以(k=1),否则开方计算,因为M最大为1e18,不可能包含3个大于1e6的质因子. #include <cstdio> #

Codeforces A. Valera and X 题解

判断二维字符串是否满足下面条件: on both diagonals of the square paper all letters are the same; all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. Help Valera, write the progra

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When

codeforces A. Shaass and Oskols 题解

Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delici

Codeforces Round #FF (Div. 2) 题解

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n 

Codeforces D. Giving Awards 412 题解

就是按照一定顺序输出排序. 比如a欠b的钱就不能先输出a然后输出b. 本题的技巧就是,要求的是不能先输出a然后输出b,但是可以先输出b然后输出a. 故此可以按照a欠b的钱的关系,建立图,然后DFS深度优先搜索,然后逆向记录点,输出这些逆向点,也就是a欠b的钱,就先输出b然后输出a,那么这个顺序就满足要求了. 很狡猾的题意.要细心.不然就搞半天都白搞了. 题目连接:http://codeforces.com/problemset/problem/412/D #include <stdio.h>