C Insertion Sort
想了半年没想出来,看看巨佬的的吧
注:
突然发现自己想法大致是对来
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve(int t)
{
ll n,k,q;scanf("%lld%lld%lld",&n,&k,&q);
// k = min (n,k);
if (k >= n) {
ll ans = 1;
for (ll i = 1;i <= n;i ++) {
ans = ans * i % q;
}
printf("Case #%d: %lld\n",t,ans);
} else {
ll ans = 1;
for (ll i = 1;i <= k;i ++) {
ans = ans * i % q;
}
ll jiecheng = ans;
ans = ((n-k) *(k+1) + (n-k-1)*(n-k-1)) * jiecheng %q;
// ans =(ans + (k * (n-k) + (n-k) * (n-k-1) / 2) * jiecheng% q) % q;
// ans =(ans + jiecheng * (n-k)) % q;
// ans = (ans-jiecheng+q)%q;
printf("Case #%d: %lld\n",t,ans);
}
}
int main()
{
ll ans = 0;
int t;scanf("%d",&t);
for(int i= 1;i <= t;i ++) {
solve(i);
}
}
- 思路
1.全排列 —— k ! k! k!
2.剩下的那个元素出现在了 n − k n-k n−k,
那么就是 ( ( n − k ) ∗ k + ( n − k − 1 ) ( n − k ) ) ∗ k ! ((n-k)*k+(n-k-1)(n-k))*k! ((n−k)∗k+(n−k−1)(n−k))∗k!
分两部分, 1 − k 1-k 1−k出现在了后面, n − k n-k n−k 后面有其中一个数出现逆序的问题(就是这里想岔了,每一个数都可以有 n − k n-k n−k 个位置,不用除以2。哎哎哎,一晚上又没了。。。回去写计组了。。。
3.剩下的那个元素出现了 1 − k 1-k 1−k
那么就是 ( n − k ) ∗ k ! (n-k)*k! (n−k)∗k!
但是这样算的话,相邻的会有一个重复的,就是 k 和 k + 1 k和k+1 k和k+1 ,所以上述三种情况加起来后在减去一个 k ! k! k!。
签到题
不写了
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e7;
// const ll MAX = 5010*5010*5010;
ll count_byte(string s)
{
if (s[0] == 'b'||s[0] == 'c') return 1;//bool char
if (s[0] == 'f'||s[0] == 'i') return 4;//int float
if (s[0] == '_') return 16; //__int128
if (s[0] == 'd') return 8; // double
if (s[8] == 'g') return 8; //long long
return 16; //long double
}
void solve(int t)
{
int n;cin>>n;
string s;cin.ignore();
ll ans = 0;
while(n --) {
getline(cin,s);
// cout<<s<<endl;
if (s.find("[") != s.npos) {
ll cnt = 0;
int i;
for (i = 0;i < s.size();i ++) {
if (s[i] == '[') break;
}
for (int j = i + 1;j < s.size();j ++) {
if (s[j] == ']') break;
else {
cnt = cnt * 10 + s[j] - '0';
}
}
// cout<<"cnt"<<cnt<<endl;
ans += cnt * count_byte(s);
} else {
ans += count_byte(s);
}
}
if (ans % 1024 == 0) {
ans /=1024;
} else {
ans = ans / 1024 + 1;
}
printf("Case #%d: %lld\n",t,ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t;cin>>t;
for(int i= 1;i <= t;i ++) {
solve(i);
}
}