Gym 100541 B. 思维题 找规律

Write a program to compute the following sum S given a positive integer n:

, where  is the largest integer not greater than x.

Input 

The input file consists of several datasets. The first line of the input file contains the number of datasets which is a positive integer and is not greater than 30. The following lines describe the datasets.

Each dataset contains a positive integer n (n ≤ 1012) written on a separate line.

Output 

For each dataset, write in one line the remainder of the computed sum S divided by 106. 

Input 

2
1
5

Output

1

10 

My Solution

写几个n试试看, 会有连续的数字出现, 

比如

n == 5, 5 2 1 1 1

n == 8, 8 4 2 2 1 1 1 1

相当于把这些数据分成好多块, 最多sqrt(n)份,区别于数据结构分块的另外一种分块吧^_^

len 表示相同数字的个数, 然后i表示这些相同val (LL)的第一个数的下标, 

ans + len*val //然后注意每一步取模

i += len; //直接跳到下一种val的第一个数

其中 len = n / val - i + 1

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int Hash = 1e6;
 
inline LL mod(LL x)
{
    return x - x/Hash*Hash;
}
 
int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    //freopen("b.txt", "w", stdout);
    #endif // LOCAL
    int T;
    LL n, val, len, ans;   //len 表示一串相同的 val 的长度
    scanf("%d", &T);
    while(T--){
        ans = 0;
        scanf("%I64d", &n);
        for(LL i = 1, j; i <= n; i += len){
            val = n/i;
            len = n/val - i + 1;
            ans = mod(ans + mod(len*val));
        }
        printf("%I64d\n", ans);
 
    }
    return 0;
}

 来源:https://blog.csdn.net/ProLightsfxjh/article/details/52031832

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值