牛客练习赛99—C
数的和与积
https://ac.nowcoder.com/acm/contest/34330/C
思路:
2和4是不可以的,除了3意外其他的都可以用三项解决,其中一项为1,剩余两项分别设为x,y. sum=x+y+1+x*y。可以化简成sum=(x+1)(y+1)。
前n项的和为n(n+1)/2,可以等位置替换一下,其中奇偶是不同的,因为当奇数除2时会又偏差,所以要把他们都转化为偶数。
奇数:sum=((n+1)/2-1+1)*(n-1+1).
偶数:sum=(n/2-1+1)*(n+1).
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; const int N=5e5+5; ll vis[N],a[N],ans,temp[N]; priority_queue<ll,vector<ll>,greater<ll>> q; signed main(){ ios::sync_with_stdio(false); cin.tie(0); ll t;cin>>t; while(t--){ ll n;cin>>n; if(n==2||n==4) cout<<"-1"<<endl; else if(n==3) cout<<"1"<<endl<<"3"<<endl; else if(n&1) cout<<3<<endl<<1<<" "<<(n-1)/2<<" "<<(n-1)<<endl; else cout<<3<<endl<<1<<" "<<(n-2)/2<<" "<<n<<endl; } }

浙公网安备 33010602011771号