https://www.luogu.org/problemnew/show/P1762

题意:给定一个正整数n,请输出杨辉三角形前n行的偶数个数对1000003取模后的结果。

由于N <= 1e15,这就暗示我们这是一道需要打表找规律的图。

年轻的花花以为求偶数个数就应当打偶数个数的表,不料这题的规律在于奇数。

所以一张完整的表应当把偶数个数,偶数个数和,奇数个数,奇数个数和,总数全部表示出来。

当行数为2 ^ k时,该行的奇数为2 ^ k个,即全部为奇数,该行的奇数和为3 ^ k 个。

所以当行数为2 ^ k的形式的时候,可以很容易的通过求和公式算出总个数再减去奇数的方式来计算答案。

现在问题要扩展到行数不满足条件的时候

规律就是将行数分为 p = 2 ^ k1 + 2 ^ k2 ....... + 2 ^kn的形式(kn > kn - 1 > .... > k2 > k1)

易得这样的形式唯一,第p行的奇数和就是 1 * (3 ^ kn) + 2 * (3 ^ kn - 1 ) + ... + pow(2,n - 1) * (3 ^ k1)次。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
inline int read(){int now=;register char c=getchar();for(;!isdigit(c);c=getchar());
for(;isdigit(c);now=now*+c-'',c=getchar());return now;}
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
const double eps = 1e-;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = ;
LL N;
int cnt;
LL quick_power(LL a,LL b){
LL ans = ;
while(b){
if(b & ) ans = (ans * a) % mod;
b >>= ;
a = (a * a) % mod;
}
return ans;
}
LL solve(int x){
LL sum = quick_power(,x) * quick_power(,cnt++) % mod;
//cout << x << " " << sum << endl;
return sum;
}
int main(){
Scl(N);
LL ans = ;
cnt = ;
for(int i = ; i >= ; i --){
if(N & (1LL << i)) ans = (ans + solve(i)) % mod;
}
LL sum = (((N + ) % mod) * (N % mod)) / % mod;
sum = ((sum - ans) % mod + mod) % mod;
Prl(sum);
return ;
}

洛谷P1762 杨辉三角,规律的更多相关文章

  1. 816D.Karen and Test 杨辉三角 规律 组合

    LINK 题意:给出n个数,每个数对间进行加或减,结果作为下一层的数,问最后的值为多少 思路:首先我们发现很像杨辉三角,然后考虑如何计算每个数对结果的贡献值,找规律可以发现当数的个数为偶数时,其所在层 ...

  2. java编写杨辉三角

    import java.util.Scanner; /* *计算杨辉三角: * 规律:两边都是1 * 从第三行开始,上一行的前一个元素+与其并排的元素等于下面的元素 * 例如: * 1 * 11 * ...

  3. 洛谷U14200 Changing 题解 【杨辉三角】

    题目描述 有nnn盏灯环形排列,顺时针依次标号为1⋯n1\cdots n1⋯n.初始时刻为000,初始时刻第iii盏灯的亮灭aia_iai​给定,000表示灭,111表示亮.下一时刻每盏灯的亮灭取决于 ...

  4. 杨辉三角 x

    杨辉三角是美丽的数学结晶,其结论往往多蕴含自然之美. ——以下内容均摘抄自题解. 例题: 洛谷P1762  偶数 正如这题所示,数据在n<=10^15的范围内则引导我们去寻找空间更节省,速率更高 ...

  5. 2021.07.19 P2624 明明的烦恼(prufer序列,为什么杨辉三角我没搞出来?)

    2021.07.19 P2624 明明的烦恼(prufer序列,为什么杨辉三角我没搞出来?) [P2624 HNOI2008]明明的烦恼 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn ...

  6. HDOJ(HDU) 1799 循环多少次?(另类杨辉三角)

    Problem Description 我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分.例如, 如果代码中出现 for(i=1;i<=n;i++) OP ; 那么做了n次 ...

  7. 基于visual Studio2013解决C语言竞赛题之0509杨辉三角

     题目

  8. 51nod 1118 机器人走方格 解题思路:动态规划 & 1119 机器人走方格 V2 解题思路:根据杨辉三角转化问题为组合数和求逆元问题

    51nod 1118 机器人走方格: 思路:这是一道简单题,很容易就看出用动态规划扫一遍就可以得到结果, 时间复杂度O(m*n).运算量1000*1000 = 1000000,很明显不会超时. 递推式 ...

  9. Java数组的应用:案例:杨辉三角,三维数组,字符串数组

    //import java.util.Arrays; //包含Arrays //import java.util.Random; public class HelloWorld { public st ...

随机推荐

  1. 当页面上需要的字段不在model中时候,需要自行设置该字段

    当页面上需要的字段不在model中时候,需要自行设置该字段

  2. hdu-2222(ac自动机模板)

    题意:给你一个长度为n的单词表,一个文本串,问你这个文本串中出现了单词表中多少个单词: 解题思路:ac自动机的模板题,可以直接当模板用: 代码: #include<iostream> #i ...

  3. servlet篇 之 访问形式

    get方式访问和post方式访问: get/post区别? 1) 参数传递 查询字符串(的形式)! get  url?key1=value&key2=value 2) http协议 请求报文包 ...

  4. 使用coobird Thumbnailator生成缩略图

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  5. [踩过的坑]Elasticsearch.Net 官网示例的坑

    经过昨天的ElasticSearch 安装,服务以及可以启动了,接下来就可以开发了,找到了官网提供的API以及示例,Es 官方提供的.net 客户端有两个版本一个低级版本: [Elasticsearc ...

  6. Python中的urllib2模块解析

    Name urllib2 - An extensible library for opening URLs using a variety of protocols 1. Description Th ...

  7. 牛客寒假算法训练1 D 欧拉(容斥)

    1 #include<bits/stdc++.h> using namespace std; ; typedef long long ll; int p[maxn],a[maxn]; ll ...

  8. CF1106F Lunar New Year and a Recursive Sequence

    题目链接:CF1106F Lunar New Year and a Recursive Sequence 大意:已知\(f_1,f_2,\cdots,f_{k-1}\)和\(b_1,b_2,\cdot ...

  9. bzoj 1854: [Scoi2010]游戏 (并查集||二分图最大匹配)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 写法1: 二分图最大匹配 思路:  将武器的属性对武器编号建边,因为只有10000种 ...

  10. MT【293】拐点处切线

    (2018浙江高考压轴题)已知函数$f(x)=\sqrt{x}-\ln x.$(2)若$a\le 3-4\ln 2,$证明:对于任意$k>0$,直线$y=kx+a$ 与曲线$y=f(x)$有唯一 ...