Array Challenge

2024-03-03 04:58
文章标签 array challenge

本文主要是介绍Array Challenge,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Array Challenge

Problem Description

There’s an array that is generated by following rule.
h 0 =2,h 1 =3,h 2 =6,h n =4h n1 +17h n2 12h n3 16 
And let us define two arrays b n anda n   as below.
b n =3h n+1 h n +9h n+1 h n1 +9h 2 n +27h n h n1 18h n+1 126h n 81h n1 +192(n>0) 
a n =b n +4 n  
Now, you have to print (a n )  , n>1.
Your answer could be very large so print the answer modular 1000000007.

Input

The first line of input contains T (1 <= T <= 1000) , the number of test cases.
Each test case contains one integer n (1 < n <= 10 15   ) in one line.

Output

For each test case print &#8970;√(a_n )&#8971; modular 1000000007.

Sample Input

3
4
7
9

Sample Output

1255
324725
13185773
看起来非常难。

试着用程序列出了一下h和a的前几个数然后就发现h的公式稍微改一下就是a的公式,稍微有个坑,算an的值直接取模的话会有负数的情况,所以先加上mod再取模就可以了。


h n =4h n1 +17h n2 12h n3 16 

a n =4a n1 +17a n2 12a n3 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define mod 1000000007
//http://acm.split.hdu.edu.cn/diy/contest_show.php?cid=32494
using namespace std;
typedef long long ll;struct matrix{int row,col;ll A[20][20];matrix(int Row=0,int Col=0){memset(A,0,sizeof(A));row=Row;col=Col;}
};matrix operator *(matrix a,matrix b){matrix c(a.row,b.col);for(int i=0;i<a.row;i++)for(int j=0;j<b.col;j++)for(int k=0;k<a.col;k++)c.A[i][j]=(c.A[i][j]+a.A[i][k]*b.A[k][j]+mod)%mod;return c;
}matrix matrix_pow(matrix a,ll n){matrix ans(a.row,a.col);for(int i=0;i<a.row;i++)ans.A[i][i]=1;while(n){if(n%2)ans=a*ans;a=a*a;n/=2;}return ans;
}int main()
{ll n,T;matrix a(3,3);a.A[0][0]=4;a.A[0][1]=17;a.A[0][2]=-12;a.A[1][0]=1;a.A[1][1]=0;a.A[1][2]=0;a.A[2][0]=0;a.A[2][1]=1;a.A[2][2]=0;matrix x1(3,3),xn(3,3);x1.A[0][0]=1255;x1.A[1][0]=197;x1.A[2][0]=31;scanf("%lld",&T);while(T--){scanf("%lld",&n);xn=matrix_pow(a,n-2)*x1;printf("%lld\n",xn.A[2][0]);}return 0;} 




这篇关于Array Challenge的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/768516

相关文章

【uva】11536-Smallest Sub-Array(区间移动问题)

一个区间移动的问题,1A了,感觉没什么好说的。。 13975926 11536 Smallest Sub-Array Accepted C++ 0.809 2014-08-01 11:00:20 #include<cstdio>#include<cstring>#include<iostream>using namespace std;#define INF 1 << 30

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

做一个问卷考试,标准答案对比用户填写的答案,array_diff 进行差集比对

if( empty(array_diff($answer_mark, $answer)) && empty(array_diff( $answer,$answer_mark))){//用户答题正确}else{// 答题错误} 做一个问卷考试,标准答案对比用户填写的答案,array_diff  进行差集比对   如用户填写的答案变量为answer   标准答案为answer_mark

UVa1104/LA5131 Chips Challenge

UVa1104/LA5131 Chips Challenge 题目链接题意分析AC 代码上下界循环费用流版本优化建图版本 题目链接   本题是2011年icpc世界总决赛的D题 题意   在一个N×N(N≤40)网格里放芯片。其中一些格子已经放了芯片(用C表示),有些格子不能放(用/表示),有些空格子可以放或者不放芯片(用.表示)。还要放一些芯片(用W表示),使得第i行的总

[LeetCode] 238. Product of Array Except Self

题:https://leetcode.com/problems/product-of-array-except-self/description/ 题目 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all

[LeetCode] 215. Kth Largest Element in an Array

题:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not th

MyBatis - 使用foreach迭代List/Array的说明

在 MyBatis 中的 foreach 元素,主要用于迭代 集合数据 以动态生成执行语句;主要有 item、index、collection、open、separator、close 等属性 属性说明         collection:要迭代的数据集对象,必填项         item:迭代出的元素的别名,必填项         index:元素的序号(map时为k

LeetCode - 33. Search in Rotated Sorted Array

33. Search in Rotated Sorted Array  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个数组,这个数组是由两个有序的数组拼接成的(无重复元素),在这个数组中查找元素k的下标. anal

[置顶]后缀数组(suffix array)详解

写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具。 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料。 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, 能够实现后缀树的很多功能而时间复杂度也不太逊色,并且,它比后缀树所占用的空间小很多。 可以说,在信息学竞赛中后缀数组比后缀树要更为实用! 因此在本文中笔者想介绍一下后缀数组的基本概念、构造

【CodeForces】266E More Queries to Array... 线段树

E. More Queries to Array... time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output You've got an array, consisting of n