HDU5651 xiaoxin juju needs help(逆元)
source link: https://arminli.com/hdu5651/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
HDU5651 xiaoxin juju needs help(逆元)
March 27, 2016
题意:随意打乱顺序,求能构成回文串的个数。
判断一下能计算的条件,方法是 strlen(l)/2 的阶乘除以每个字母出现次数一半的阶乘的积。
逆元:在 MOD 的情况下, (a/b ) %MOD 不能直接 / b 来求,需要找到一个数 inv 使得 inv * b % MOD = 1 。 这样 (a / b) % MOD = (a * inv) % MOD;
#include<cstring> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<string> #include<iostream> #include<algorithm> using namespace std; const long long mod = 1e9+7; long long a[1005]; int cnt[30]; void init(){ a[0] = 1; a[1] = 1; for(int i = 2; i <= 1000; i++) a[i] = (i*a[i-1])%mod; } long long quickmod(long long a, long long n){ long long r = 1; while(n){ if(n&1){ r = (a*r)%mod; } a = (a*a)%mod; n >>= 1; } return r; } int main(){ //freopen("a.txt", "r", stdin); int n; cin >> n; init(); while(n--){ // v.clear(); char s[1005]; scanf("%s", s); memset(cnt, 0, sizeof(cnt)); int l = strlen(s); for(int i = 0; i < l; i++){ cnt[s[i]-'a']++; } int k = 0; for(int i = 0; i < 26; i++){ if(cnt[i]%2) k++; } if(l==1) cout << "1" << endl; else if((k==1 && l%2==1) || (l%2==0 && k==0)){ long long ans = a[l/2]; //cout << ans << endl; for(int i = 0; i < 26; i++){ ans = (ans*quickmod(a[cnt[i]/2], mod-2))%mod; //cout << ans << endl; } cout << ans << endl; }else{ cout << "0" << endl; } } return 0; }
Recommend
-
3
Using LXD with Juju Choosing LXD as the backing cloud for Juju is an efficient way to experiment with Juju. It is also very quick to set up. With lightweight containers acting as Juju mac...
-
1
swift storage #272 Supports:...
-
11
Armin's BlogHDU1576 A/B(逆元)March 29, 2016题目链接 欧拉函数方法求逆元。 #include<cstdio> #include<cstr...
-
11
Lenovo Lenovo Xiaoxin Pro 14 detailed: Intel 12th CPU, 2.8K 120 Hz display and more...
-
7
Lenovo Xiaoxin Pad Pro 2022 is a new 2.5K OLED display Android tablet that comes with choices of color and processor
-
6
In a few days, Xiaomi will introduce its new Xiaomi Book Air 13 in China. The device will try to conquer users with a super-slim design, gorgeous 2.8K display, and power. Lenovo, on...
-
125
Question [TB132FU] How to install Stock Global ROM for Xiaoxin Pad Pro 2022 (Mediatek Chipset) ?...
-
128
Lenovo Xiaoxin pad 2022...
-
4
逆元素,是指一个可以取消另一给定元素运算的元素 具体来说,对于实际的一些应用,如: 当我们想要求(11 / 3) % 10时 明显可以看出,是没有办法直接算的,这时就需要引入逆元
-
6
设整数 p≠0,±1 。如果
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK