HDU 6304 Chiaki Sequence Revisited(二分+找规律)
题目链接
Chiaki Sequence Revisited
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2106 Accepted Submission(s): 585
Problem Description
Chiaki is interested in an infinite sequence a1,a2,a3,..., which is defined as follows:
an={1an−an−1+an−1−an−2n=1,2n≥3
Chiaki would like to know the sum of the first n terms of the sequence, i.e. ∑i=1nai. As this number may be very large, Chiaki is only interested in its remainder modulo (109+7).
Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤105), indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤1018).
Output
For each test case, output an integer denoting the answer.
Sample Input
10
1
2
3
4
5
6
7
8
9
10
Sample Output
1
2
4
6
9
13
17
21
26
32
题意:
给你一个序列,序列的公式在上面,然后输入一个n,让你输出a[1..n]的和,即他的前缀和
解析:
这种题一开始都是打表找规律,队友找到的规律是a[i]的值是按规律递增的,并且每一个值出现的次数都是他分解成k=2^w*p(w是该数最大的能被2整除的最大幂数)后,w+1次。譬如说12=2^2*4,所以12这个数会出现3次并且是连续出现的,即有3项a[i]=12(i=l,l+1,l+2),l未知。
把他们列出来就可以找到规律了。
1 3 5 7 9 ....... 出现1次的差为2的等差数列
2 6 10 14 18..... 出现2次的差为4的等差数列
4 12 20 28 36 ..... 出现3次的差为8的等差数列
8 24 40 56 72 .... 出现4次的差为16的等差数列
........
这样我们只要找到出现1次的等差数列的长度、出现2次的等差数列的长度、出现3次的等差数列的长度、......
我们就可以得到前缀和了。
那么我们首先就该知道a[n]这个数的值,这样才能求出上面各个等差数列的长度。
所以我们就二分它,因为你再观察一下这个数列就可以发现,这个每一个a[i]的值一定是在[i/2,i]之间的。
我们就二分它,那么如何验证。
首先知道一个数的值12=4*3,我们其实就可以得到各个等差数列的长度了。
1*12 ....(1,3,5,7,9,11)出现1次的等差数列长度为6
2*6 ....(2*1,2*3,2*5) 出现2次的等差数列长度为3
4*3....(4*1,4*3) 出现3次的等差数列长度为2
8*1.5 ....(8*1) 出现4次的等差数列长度为1
这样各个等差数列的长度就得到了。
那么验证的时候我们只要通过我们每次二分出来的值,来算出上述的长度。
并且这里需要注意因为n的范围是1e18,所以当最后得到a[n]的值计算前缀和的时候,作乘法的两个乘数都是需要模的,不然会爆long long !!!
ans=(tmp%MOD*(n%MOD))%MOD,这样子。
#include <cstdio>
#include <cstring>typedef long long ll;
const int MOD = 1e9+7;inline void solve(ll x,ll &a,ll &b)
{ll y=x;ll res=y/2+y%2;ll cnt=1;while(y%2==0){cnt++;y=y>>1;res+=(y/2+y%2)*cnt;}ll tmp=cnt;while(y){cnt++;y=y>>1;res+=(y/2+y%2)*cnt;}a=res-tmp+2;b=res+1;}inline ll cal(ll y,ll x)
{ll pre=y;ll res=y/2+y%2;ll ans=(res%MOD+(res%MOD*((res-1)%MOD))%MOD+1)%MOD;ll cnt=1;ll t2=1;ll pp,tmp;while(y%2==0){t2=t2*2%MOD;cnt++;y=y>>1;if(y%2==0){pp=(y/2+y%2);tmp=(((pp%MOD)*(t2%MOD))%MOD+((pp%MOD*((pp-1)%MOD))%MOD*(t2%MOD))%MOD)%MOD;tmp=((tmp%MOD)*(cnt%MOD))%MOD;}else{pp=(y/2+y%2)-1;tmp=((pp%MOD*(t2%MOD))%MOD+((pp%MOD*((pp-1)%MOD))%MOD*(t2%MOD))%MOD)%MOD;tmp=(tmp%MOD*(cnt%MOD))%MOD;tmp=(tmp+(pre%MOD*(x%MOD))%MOD)%MOD;}ans=(ans+tmp)%MOD;}while(y){t2=t2*2%MOD;cnt++;y=y>>1;pp=(y/2+y%2);tmp=((pp%MOD*(t2%MOD))%MOD+((pp%MOD*((pp-1)%MOD))%MOD*(t2%MOD))%MOD)%MOD;tmp=((tmp%MOD)*(cnt%MOD))%MOD;ans=(ans+tmp)%MOD;}return ans;
}ll bin_search(ll x)
{ll l,r;l=x/2;r=x;while(l<r){ll mid=(l+r)>>1;ll a,b;solve(mid,a,b);if(b<x){l=mid+1;}else if(a>x){r=mid;}else{return cal(mid,x-a+1);break;}}/*ll a,b;solve(l,a,b);return cal(l,x-a+1);*/
}int main()
{int t;scanf("%d",&t);while(t--){ll x;scanf("%lld",&x);if(x==1) printf("1\n");else if(x==2) printf("2\n");else{printf("%lld\n",bin_search(x));}}return 0;
}
HDU 6304 Chiaki Sequence Revisited(二分+找规律)相关推荐
- HDU 6304 Chiaki Sequence Revisited(找规律)
题目链接:Chiaki Sequence Revisited 题意 定义一个序列 an={1an−an−1+an−1−an−2n=1,2n≥3an={1n=1,2an−an−1+an−1−an−2n≥ ...
- HDU 6304 Chiaki Sequence Revisited
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6304 多校contest1 Problem Description Chiaki is inter ...
- [HDU-6304] Chiaki Sequence Revisited
Chiaki Sequence Revisited http://acm.hdu.edu.cn/showproblem.php?pid=6304 an={1an−an−1+an−1−an−2n=1,2 ...
- HDU - 1284 钱币兑换问题 (找规律/完全背包)
Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Out ...
- HDU 2515 Yanghee 的算术【找规律】
题意:中文的题目 找规律可以发现 sum[1]=a[1]+a[2] sum[2]=a[1]+a[3] sum[n]=a[2]+a[3] 解出a[1],就可以求出其他的了 1 #include<i ...
- Common Number(奇偶二分+找规律)
题意:给定函数f(x),x为偶数时f(x)=x/2,x为奇数时f(x)=x-1 给定n,k,对1到n每个数求f(x)的轨迹,如path[15]={15,14,7,6,3,2,1},求在所有轨迹里出现次 ...
- 【HDU - 5882】Balanced Game (找规律,思维)
题干: Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each pla ...
- HDU 5703 Desert 水题 找规律
HDU 5703 Desert 水题 找规律 已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现 ...
- 【HDU - 1564 】Play a game (博弈问题,找规律,奇偶博弈)
题干: New Year is Coming! ailyanlu is very happy today! and he is playing a chessboard game with 8600 ...
最新文章
- Apache Druid Console 远程命令执行漏洞
- 每天更新bing首页图片为桌面壁纸
- 11_HTML5_Local_Storage本地存储
- Mycat-server-1.6.5 常见分片方式
- C语言 — 编程规范
- POJ1039 Pipe
- 使用U盘制作win10启动盘
- 人人开源项目搭建到服务器,人人开源框架的部署
- 关于jxbrowser-7.19
- 【Websocket编程】基于libwebsockets实现客户端数据通信
- 阿里P7大牛手把手教你!java全栈工程师证书
- Selenium面试问题及答案30题版
- STM32 f103搭配LM386声音传感器实现简单音乐识别
- vb.net 教程 1-9 数组3-1 数组的合并
- Linux无桌面环境下,实现USB的自动挂载/卸载
- # 个人日记-电影《盛夏未来》观后感-20210802
- java有哪些注解_JAVA常用注解
- Robinson Crusoe chapter 8 A ship arrives at the island
- vue 中使用axios
- Linux命令对应英文全称