hdu 5439(找规律)
The sequence is generated by the following scheme.
1. First, write down 1, 2 on a paper.
2. The 2nd number is 2, write down 2 2’s (including the one originally on the paper). The paper thus has 1, 2, 2 written on it.
3. The 3rd number is 2, write down 2 3’s. 1, 2, 2, 3, 3 is now shown on the paper.
4. The 4th number is 3, write down 3 4’s. 1, 2, 2, 3, 3, 4, 4, 4 is now shown on the paper.
5. The procedure continues indefinitely as you can imagine. 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, . . . .
所求答案为前n项i*f[i]的和,f[i] 有i个,所以1e9大概需要算到5e5就行(参考别人的思路),
upper_bound查找(好像是二分查找),自己按思路写的最开始用的for查找,发现比别人慢很多,然后才注意到这个函数,以前虽然知道,但并没怎么用过- -。
预处理:sum存到i时数的个数,g保存到i最后一个时的i*f[ i ]值。因为n找到后不一定是i的最后一个,再填上多出部分即可
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<functional>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=5e5; int a[maxn];
ll sum[maxn];
int g[maxn]; int su(int a,int b)
{
int ans =(ll)(a+b)*(b-a+1)/2%mod;
return ans;
} int main()
{
a[1] = sum[1] = g[1] = 1;
sum[2] = 3;
g[2] = 11;
a[2] = a[3] = 2;
int cnt = 3;
for(int i = 3; i < maxn; i++)
{
for(int j = 0; j < a[i]; j++)
{
if(cnt == maxn)
break;
a[++cnt] = i;
}
sum[i] = (sum[i-1] + a[i]);
g[i] = (g[i-1]+ (ll)i*su(sum[i-1]+1,sum[i])) % mod;
} int T,n,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int tmp = n;
int cur;
for(i = 1; i < maxn; i++)
{
if(sum[i]>n){
cur = i-1;
break;
}
} ll ans = g[cur] + (ll)(cur+1)*su(sum[cur]+1,tmp)%mod;
printf("%d\n",ans%mod);
} return 0;
}
hdu 5439(找规律)的更多相关文章
- hdu 5051 找规律?+大trick
http://acm.hdu.edu.cn/showproblem.php?pid=5051 打表找规律 据说是http://zh.wikipedia.org/wiki/%E6%9C%AC%E7%A6 ...
- HDU 4731 找规律,打表
http://acm.hust.edu.cn/vjudge/contest/126262#problem/D 分为3种情况,n=1,n=2,n>=3 其中需要注意的是n=2的情况,通过打表找规律 ...
- 汉诺塔问题hdu 2065——找规律
这类题目就是纸上模拟,找规律. 问题描述:在一块铜板上有三根杆,目的是将最左边杆上的盘全部移到右边的杆上,条件是不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允 ...
- hdu 5229 找规律
假设选择了字符串a和b: 假设两人都按照最聪明的策略,那么观察一下可以找出规律:当a和b的字符串长度之和为奇数的时候zcc会败. 另外当a==b的时候zcc也会败(当时做的时候忘了这个了T^T) 接下 ...
- HDU 2147 找规律博弈
题目大意: 从右上角出发一直到左下角,每次左移,下移或者左下移,到达左下角的人获胜 到达左下角为必胜态,那么到达它的所有点都为必败态,每个点的局势都跟左,下,左下三个点有关 开始写了一个把所有情况都计 ...
- HDU 1564 找规律博弈
题目大意是: 从n*n的方格角落的一个起点出发,每次移到上下左右一个未曾到达过的位置,谁不能走了谁就输了 想了好久都想不出,看了大神的题解 Orz了 果然博弈不是脑残的游戏啊... 这里从起点出发,将 ...
- 2019CCPC网络赛 HDU 6702——找规律
题意 给定 $A,B$(都是正整数),求使得 $(A\ xor\ C) \& (B \ xor \ C)$ 最小的正整数 $C$,如果有多个满足条件的 $C$,输出最小的 $C$. 分析 ...
- 洛谷 P1014 Cantor表【蛇皮矩阵/找规律/模拟】
题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … ...
- Hdu 5439 Aggregated Counting (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online 找规律)
题目链接: Hdu 5439 Aggregated Counting 题目描述: 刚开始给一个1,序列a是由a[i]个i组成,最后1就变成了1,2,2,3,3,4,4,4,5,5,5.......,最 ...
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
随机推荐
- 201621123031 《Java程序设计》第3周学习总结
Week03-面向对象入门 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系 ...
- pandas 使用
ss = [['xx','m',22],['cc','w',33],['jj','w',44],['qq','m',11]] import pandas as pd df = pd.DataFrame ...
- MySQL搭建主从数据库 实现读写分离
首先声明,实际生产中,网站为了提高用户体验,性能等,将数据库实现读写分离是有必要的,我们让主数据库去写入数据,然后当用户查询的时候,然后在从数据库读取数据,故能减轻数据库的压力,实现良好的用户体验! ...
- 【编程开发】PHP---面向对象
面向对象编程 类:在现实世界中,任何事物都有种类的概念:车 类是由特征和行为构成的. 特征:都是不动的,从出厂的时候就已经内置好了(属性) 行为:一种动的状态.(方法(函数)) 行为依赖于这些特征,而 ...
- Spring Boot 配置文件详解
Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...
- hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(2)安装hadoop
一.依赖安装 安装JDK 二.文件准备 hadoop-2.7.3.tar.gz 2.2 下载地址 http://hadoop.apache.org/releases.html 三.工具准备 3.1 X ...
- Spring Security入门(2-3)Spring Security 的运行原理 3
关键组件关系 FilterSecurityInterceptor--- authenticationManager --- UserDetailService--- accessDecisionMan ...
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- python Mysql (二)
Mysql (二) 一. 事务 a.数据库开启事务命令 1 2 3 4 #start transaction 开启事务 #Rollback 回滚事务,即撤销指定的sql语句(只能回退insert de ...
- HTML初识
HTML初识 web服务本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) so ...