SOJ 3191 Free square

Time Limit: 5000 MS    Memory Limit: 65536 K 

Description

A positive integer is said to be squarefree if it is divisible by no perfect square larger than 1. For example, the first few squarefree numbers are {1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, ...}. Can you tell me the K-th ( 1-indexed ) smallest squarefree number.

Input

The first line of the input will be a integer to represent the number of test cases. For each test case there is only one line contains only one integer K. ( 1 <= K <= 10^9 ) There is a blank line before each test case.

Output

For each test case output the answer on a single line: The K-th smallest squarefree number.

Sample Input

6 1 2 7 1000 1234567 1000000000

Sample Output

1 2 10 1637 2030745 1644934081

Source

判断每一个数的质因子个数是奇数还是偶数还是有一个以上相同的质因子,以后用来容斥

二分出最小的x:1到x内恰有k个squarefree

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
vector<int>prime;
bool vis[300];
void make_p()
{
    for(int i=2;i<300;i++)
        if(!vis[i])
        {
            prime.push_back(i);
            for(int j=i+i;j<300;j+=i)
                vis[j]=1;
        }
}
int flag[40558];
int make_f()
{
    for(int i=2;i<40558;i++)
    {
        int cnt=0,t=i;
        for(int j=0;prime[j]*prime[j]<=t;j++)
        {
            if(t%prime[j]==0)
            {
                cnt++;
                t/=prime[j];
                if(t%prime[j]==0)
                {
                    cnt=-1;
                    break;
                }
            }
        }
        if(cnt!=-1&&t>1)
            cnt++;
        flag[i]=cnt==-1?0:cnt&1?-1:1;
    }
}
int work(int x)
{
    int l=1,r=1644934081;
    while(l<=r)
    {
        int sum,m;
        sum=m=l+(r-l)/2;
        for(int i=2;m/i/i>0;i++)
            sum+=flag[i]*m/(i*i);
        if(sum<x)
            l=m+1;
        else
            r=m-1;
    }
    return l;
}
int main()
{
    make_p();
    make_f();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int k;
        scanf("%d",&k);
        printf("%d\n",work(k));
    }
}
时间: 2024-10-14 22:13:56

SOJ 3191 Free square的相关文章

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

Sicily 1302 Magic Square (数论)

链接:http://soj.me/1302 题目中有图和表格,所以就不把题目信息列出来了,打开链接直接看就行了... 分析: 从填幻方的过程,可以判断一下几点: 1>.幻方的每一列都是向下发展的: 2>.填入的前n个数都刚好是每一列第一个填入的数: 从这两点可以推出下面几点: (1). 每一列的发展是平面的(即每一列的个数是一样的,特别的,只有一列少一): (2). 右下角那个数肯定是填入的最大的: (3). 当我们填入到右下角的时候,每一列的个数都刚好是n/2(注意n是奇数,这里的除是整除)

[leetcode] 367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 使用二分查找寻找input

Project Euler 92:Square digit chains C++

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 Therefore any chain that

LeetCode Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 思路分析:这题考察DP.缓存中间结果降低反复计算.DP方程为 if(matrix[i][j

Theatre Square 题解代码

Theatre Square CodeForces - 1A 水题水题... 最开始的程序是这个,可是AC不了我也不知道为什么......QAQ... #include <stdio.h>#include<stdlib.h>int main(){    int m,n,a,ans,tmp,k,h,tep,i,j;    scanf("%d %d %d",&m,&n,&a);    if(m%a==0)    {        if(n%a

[LeetCode] Matchsticks to Square 火柴棍组成正方形

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, a

codeforces 710C C. Magic Odd Square(构造)

题目链接: C. Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer n (1 ≤ n ≤ 49). Output Print n lines with n integers. All the

hdu1518(Square)深搜+剪枝

点击打开杭电1518 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? Input The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20,