C. Number of Ways 思维
Posted dreamkill
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C. Number of Ways 思维相关的知识,希望对你有一定的参考价值。
题目大意就是问能分出几个使三个区间的和相等的分发
1 #include <algorithm> 2 #include <stack> 3 #include <istream> 4 #include <stdio.h> 5 #include <map> 6 #include <math.h> 7 #include <vector> 8 #include <iostream> 9 #include <queue> 10 #include <string.h> 11 #include <set> 12 #include <cstdio> 13 #define FR(i,n) for(int i=0;i<n;i++) 14 #define MAX 2005 15 #define mkp pair <int,int> 16 using namespace std; 17 #include <bits/stdc++.h> 18 const int maxn = 5e5 + 40; 19 typedef long long ll; 20 const int inf = 0x3fffff; 21 void read(ll &x) { 22 char ch; bool flag = 0; 23 for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == ‘-‘)) || 1); ch = getchar()); 24 for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar()); 25 x *= 1 - 2 * flag; 26 } 27 28 29 ll n; 30 ll sum[maxn],arr[maxn]; 31 ll val[maxn]; 32 ll s2[maxn]; 33 ll l[maxn]; 34 int main() { 35 read(n); 36 for(int i=0;i<n;i++)read(val[i]); 37 sum[0]=val[0]; 38 for(int i=1;i<n;i++)sum[i]+=sum[i-1]+val[i]; 39 //int i = 0,j = n-1; 40 for(int i=n-1;i>=0;i--)s2[i]+=s2[i+1]+val[i]; 41 if(sum[n-1]%3)return 0*printf("%d ",0); 42 ll temp = 0; 43 for(int i=0;i<n;i++){ l[i]=temp; 44 if(sum[i]==sum[n-1]/3)temp++; 45 46 } 47 ll ans = 0; 48 for(int i=n-1;i>=1;i--){ 49 if(s2[i]==sum[n-1]/3){ 50 ans+=l[i-1]; 51 } 52 // cout<<ans<<endl; 53 } 54 cout<<ans<<endl; 55 return 0; 56 }
以上是关于C. Number of Ways 思维的主要内容,如果未能解决你的问题,请参考以下文章
CF 466C Number of Ways(数学 / 思维 / DP)
[CodeForces 466C] Number of Ways
1411. Number of Ways to Paint N × 3 Grid
Leetcode 1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers
LeetCode 1269. Number of Ways to Stay in the Same Place After Some Steps