Codeforces Round #760 (Div. 3)

比赛链接

Codeforces Round #760 (Div. 3)

E. Singers' Tour

n towns are arranged in a circle sequentially. The towns are numbered from 1 to n in clockwise order. In the i-th town, there lives a singer with a repertoire of ai minutes for each i[1,n].

Each singer visited all n towns in clockwise order, starting with the town he lives in, and gave exactly one concert in each town. In addition, in each town, the i-th singer got inspired and came up with a song that lasts ai minutes. The song was added to his repertoire so that he could perform it in the rest of the cities.

Hence, for the i-th singer, the concert in the i-th town will last ai minutes, in the (i+1)-th town the concert will last 2ai minutes, ..., in the ((i+k)modn+1)-th town the duration of the concert will be (k+2)ai,, in the town ((i+n2)modn+1)nai minutes.

You are given an array of b integer numbers, where bi is the total duration of concerts in the i-th town. Reconstruct any correct sequence of positive integers a or say that it is impossible.

Input

The first line contains one integer t(1t103) - the number of test cases. Then the test cases follow.
Each test case consists of two lines. The first line contains a single integer n(1n4104) the number of cities. The second line contains n integers b1,b2,,bn(1bi109) - the total duration of concerts in i-th city.
The sum of n over all test cases does not exceed 2105.

Output

For each test case, print the answer as follows:
If there is no suitable sequence a, print NO. Otherwise, on the first line print YES, on the next line print the sequence a1,a2,,an of n integers, where ai(1ai109) is the initial duration of repertoire of the i-th singer. If there are multiple answers, print any of them.

Example

input

4 3 12 16 14 1 1 3 1 2 3 6 81 75 75 93 93 87

output

YES 3 1 3 YES 1 NO YES 5 5 4 1 4 5

Note

Let's consider the 1-st test case of the example:

  1. the 1-st singer in the 1-st city will give a concert for 3 minutes, in the 2 -nd - for 6 minutes, in the 3 -rd - for 9 minutes;
  2. the 2 -nd singer in the 1 -st city will give a concert for 3 minutes, in the 2 -nd - for 1 minute, in the 3 -rd - for 2 minutes;
  3. the 3 -rd singer in the 1 -st city will give a concert for 6 minutes, in the 2 -nd - for 9 minutes, in the 3 -rd - for 3 minutes.

解题思路

构造,线性代数

即求解如下 nn 元一次方程组:
{a1+n×a2+(n1)×a3++2×an=b12×a1+a2+n×a3++3×an=b2n×a1+(n1)×a2+(n2)×a3++an=bn

当高斯消元的复杂度为 n3,会超时,不妨先从小点的范围开始构造
构造答案前,将上面的等式加起来,有 (n+1)×n2×i=1nai=i=1nbi,则必须有 i=1nbi%(n+1)×n2=0
n=3 开始构造:
有如下等式:
{a1+2a2+2a3=b12a1+a2+3a3=b2
做差,得 a1+2a2a3=b1b2,即 a2=b1b2+a1+a2+a33
得构造得到解:a[i%n+1]=b[i]b[i%n+1]+i=1na[i]n,注意 a[i] 为正整数,且范围为 [1,109]

  • 时间复杂度:O(n)

代码

// Problem: E. Singers' Tour // Contest: Codeforces - Codeforces Round #760 (Div. 3) // URL: https://codeforces.com/contest/1618/problem/E // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=4e4+5; int n,t; LL a[N],b[N]; int main() { for(scanf("%d",&t);t;t--) { scanf("%d",&n); LL sum=0; for(int i=1;i<=n;i++)scanf("%lld",&b[i]),sum+=b[i]; int all=1ll*n*(n+1)/2; if(sum%all!=0) { puts("NO"); continue; } sum/=all; bool f=true; for(int i=1;i<=n;i++) { LL t=b[i]-b[i%n+1]+sum; if(t%n!=0) { f=false; break; } a[i%n+1]=t/n; if(!(a[i%n+1]>=1&&a[i%n+1]<=1e9)) { f=false; break; } } if(!f)puts("NO"); else { puts("YES"); for(int i=1;i<=n;i++)printf("%lld ",a[i]); puts(""); } } return 0; }

F. Reverse

You are given two positive integers x and y. You can perform the following operation with x : write it in its binary form without leading zeros, add 0 or 1 to the right of it, reverse the binary form and turn it into a decimal number which is assigned as the new value of x.
For example:

  • 34 can be turned into 81 via one operation: the binary form of 34 is 100010 , if you add 1 , reverse it and remove leading zeros, you will get 1010001 , which is the binary form of 81 .
  • 34 can be turned into 17 via one operation: the binary form of 34 is 100010, if you add 0 , reverse it and remove leading zeros, you will get 10001 , which is the binary form of 17 .
  • 81 can be turned into 69 via one operation: the binary form of 81 is 1010001 , if you add 0 , reverse it and remove leading zeros, you will get 1000101 , which is the binary form of 69 .
  • 34 can be turned into 69 via two operations: first you turn 34 into 81 and then 81 into 69 .

Your task is to find out whether x can be turned into y after a certain number of operations (possibly zero).

Input

The only line of the input contains two integers x and y(1x,y1018).

Output

Print YES if you can make x equal to y and NO if you can't.

解题思路

dfs,记忆化搜索

对于一个数二进制,如果其后面没有 0,则如果在其后面加 0 的话翻转后前导 0 会去掉,相当于翻转原二进制串,即后面只能加 1,这就相当于只能在原二进制串前后加 1,且可翻转;如果一个数的二进制后面有 0,则有两种选择:将后面的 0 去掉,在后面加 1,这时两遍 dfs 即可

  • 时间复杂度:O(32×32)

代码

// Problem: F. Reverse // Contest: Codeforces - Codeforces Round #760 (Div. 3) // URL: https://codeforces.com/contest/1618/problem/F // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } LL x,y; unordered_set<string> s; string get(LL x) { string res; do { res+='0'+(x&1); x>>=1; }while(x); return res; } void dfs(string x) { if(s.count(x)||x.size()>=64)return ; s.insert(x); reverse(x.begin(),x.end()); dfs(x); x+='1'; dfs(x); x.pop_back(); x='1'+x; dfs(x); } int main() { cin>>x>>y; string st=get(x),en=get(y); reverse(st.begin(),st.end()); reverse(en.begin(),en.end()); if(st.back()=='0') { s.insert(st); dfs(st+'1'); while(st.size()&&st.back()=='0')st.pop_back(); dfs(st); } else dfs(st); puts(s.count(en)?"YES":"NO"); return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16250728.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示