Problem G. Pyramid

 Pyramid(打表+规律)

题意:给你一个n即给定图的规模,问你图内有多少个三角形。

思路:打表,找规律,列方程,解方程。

首先打个表

#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define up(i, a, b)for(int i = a;i <= b; i++)
#define down(i, a, b)for(int i =a;i >= b; i--)
#define inf 0x3f3f3f3f
#define int long long
using namespace std;
const int maxn = 1e5 + 700;
struct node{
    double x, y;
}arr[maxn];
double dis(int x, int y){
    return sqrt((arr[x].x-arr[y].x)*(arr[x].x-arr[y].x) + (arr[x].y-arr[y].y)*(arr[x].y-arr[y].y));
}
signed main()
{
    double h = sqrt(3)/2.0;
//    cout << h << endl;
    int t,id = 1;
    while(id <= 20){
        int n;n = id;
        int ind = 0;
        if(n%2 == 0){
    //        int ind = 0;
            up(j, 0, n/2){
                up(i, 0, j){
                    arr[++ind] = (node){(double)i, (double)(n-j*2)*h};
                    if(i!=0)arr[++ind] = (node){(double)-i, (double)(n-j*2)*h};
                }
            }
            up(j, 0, n/2-1){
                up(i, 0, j){
                    arr[++ind] = (node){(double)i+0.5, (double)(n-j*2-1)*h};
                    arr[++ind] = (node){(double)-i-0.5, (double)(n-j*2-1)*h};
                }
            }
    //        up(i, 1, ind)cout << arr[i].x <<' ' << arr[i].y << endl;
        }
        else{
    //        int ind = 0;
            up(j, 0, n/2){
                up(i, 0, j){
                    arr[++ind] = (node){(double)i, (double)(n-j*2)*h};
                    if(i!=0)arr[++ind] = (node){(double)-i, (double)(n-j*2)*h};
                }
            }
            up(j, 0, n/2){
                up(i, 0, j){
                    arr[++ind] = (node){(double)i+0.5, (double)(n-j*2-1)*h};
                    arr[++ind] = (node){(double)-i-0.5, (double)(n-j*2-1)*h};
                }
            }
    //        up(i, 1, ind)cout << arr[i].x <<' ' << arr[i].y << endl;
        }
    //    up(i, 1, ind)cout << arr[i].x <<' ' << arr[i].y << endl;
//        puts("");
        int ans = 0;
        up(i, 1, ind){
            up(j, i+1, ind){
                up(k, j+1, ind){
                    double a = dis(i, j),b = dis(j, k), c = dis(i, k);
    //                cout << a <<' ' << b << ' ' << c << endl;
                    if(fabs(a-b) < 0.0000001 && fabs(a-c) < 0.0000001 && fabs(b-c) < 0.00000001){
    //                    cout << a <<' ' << b << ' ' << c << endl;
                        ans++;
                    }
                }
            }
        }
        cout << id++ <<":" <<ans << endl;

    }


    return 0;
}
/*
1 5 15 35 70 126 210 330 495 7151
*/

可得出1~10的答案分别为   1 5 15 35 70 126 210 330 495 7151

求差值列方程(拉格朗日插值公式)

推完后,注意用逆元,并在循环外定义,以免tle。

ac代码:

#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
#define up(i, a, b)for(int i = a;i <= b; i++)
#define down(i, a, b)for(int i =a;i >= b; i--)
#define inf 0x3f3f3f3f
#define int long long
using namespace std;
const int maxn = 1e5 + 700;
const int mod = 1e9 + 7;
int qpow(int a, int b){
    int ans = 1;
    while(b){
        if(b&1){ans = (ans*a)%mod;}
        a = (a*a)%mod;
        b /= 2;
    }
    return ans%mod;
}
int inv(int x){
    return qpow(x, mod-2);
}
signed main()
{
    int one = inv(24);
//    cout << qpow(2,5);
    int t;
    scanf("%lld", &t);
    while(t--){
        int n;
        scanf("%lld", &n);
        int ans = (qpow(n,4)*one%mod) + (qpow(n,3)*6*one%mod) + (qpow(n,2)*11*one%mod) + (qpow(n,1)*6*one%mod);
        printf("%lld\n", ans%mod);
    }
    return 0;
}
/*
1 5 15 35 70 126 210 330 495 715

*/

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值