zoj 3175 Number of Containers 分块加速

题意:计算n以内i的倍数的个数,不包括i本身,求和

解:直接暴力for一遍一定是不行的,不过用一个分块加速就可以了

#include <stdio.h>
#include <string.h>
int main()
{
    int t;
    long long n;
    while(scanf("%d",&t)!=-1)
    {
        while(t--)
        {
            scanf("%lld",&n);                  //要用lld错了N发的教训
            long long sum=0;
            for(int i=1,last;i<=n;i=last+1)
            {
                last=n/(n/i);                 //分块加速,所有n/i结果相同的一起计算
                sum+=((n/i-1)*(last-i+1));
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-30 08:24:45

zoj 3175 Number of Containers 分块加速的相关文章

ZOJ 2836 Number Puzzle ( 容斥原理 )

ZOJ 2836 Number Puzzle( 容斥原理 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define CLR( a, b ) memset( a, b, sizeof(a) ) int m, n, A[11]; LL gcd( LL a, LL b ) { return b == 0 ? a :

[容斥原理] zoj 2836 Number Puzzle

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 Number Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers th

ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F

Number Game Time Limit: 2 Seconds      Memory Limit: 65536 KB The bored Bob is playing a number game. In the beginning, there are n numbers. For each turn, Bob will take out two numbers from the remaining numbers, and get the product of them. There i

ZOJ 2836 Number Puzzle

Number Puzzle Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 283664-bit integer IO format: %lld      Java class name: Main Given a list of integers ($A_1, A_2, ..., A_n$), and a positive integer M, please fi

ZOJ 2836 Number Puzzle(容斥原理啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the g

ZOJ 3180 Number Game(数学啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3221 Gordon is recently reading about an interesting game. At the beginning of the game, there are three positive numbers written on a blackboard. In each round, you are asked to delete

zoj 1088题解--Josephus 问题,加速解决

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">zoj 1088题目题目大意是,对n栋楼停电,先停第一栋,再隔m栋停一栋.数到最后一栋后从头循环计数,已经断电的不参与计数.要选取适当的m,使得即使其他楼都没电了,但第二栋楼仍然有电. </span> 如果将该题视为普通的模拟算法,其时间复杂度将高达O(m*n).由于题目给了

整除分块加速取余

传送门 \(\sum_{i = 1}^{n}k \ mod \ i\) \(k \ mod \ i = k - \lfloor \frac{k}{i} \rfloor*i\) \(\sum_{i = 1}^{n}k \ mod \ i = kn - \sum_{i = 1}^{n}\lfloor \frac{k}{i}\rfloor*i\) 比整除分块模板多了i,而i在每一个分块是一个等差数列,可以用求和公式求. #include <iostream> #include <cstdio&

ZOJ 3180 Number Game(模拟,倒推)

题目 思路: 先倒推!到最后第二步,然后: 初始状态不一定满足这个状态.所以我们要先从初始状态构造出它出发的三种状态.那这三种状态跟倒推得到的状态比较即可. #include<stdio.h> #include<string.h> #include <algorithm> using namespace std; int t,a[5],b[5]; int main() { scanf("%d",&t); while(t--) { scanf(